interface, inheritance, polymorphism

Source: Internet
Author: User

interface, inheritance, polymorphism

Inheritance and polymorphism are a very important part of the object-oriented development language, and if you use inheritance and polymorphism in your program, the architecture of the entire program becomes very resilient, and notifications can reduce the redundancy of your code. The use of inheritance mechanisms can be used to reuse some well-defined classes, reducing the writing of repetitive code. The use of polymorphic mechanisms can dynamically adjust the invocation of objects, reducing the dependencies between objects.

1. The structural process of the neutron class of the inheritance mechanism

    • The constructor of a subclass is accessed by default, and the constructor for the grain-controlled parameter is accessible by the first row in the subclass of each constructed row by the implicit super ();
    • When you create a subclass object, you need to see how the parent class initializes the data, so that when the object is initialized, the subclass accesses the constructor in the parent class first.

    • If you want to access the constructor specified in the parent class, you can specify it by manually defining the Super statement.

Of course:

The constructor of a subclass the first row can also manually specify the this statement to access the constructors in this class, and at least one constructor in the subclass accesses the constructor in the parent class.

Class Zi extends Fu{zi () {super ();  Implicitly add System.out.println ("Zi Run");} Zi (int x) {this (); System.out.println ("Zi ..." +x);}

2. Overwriting and rewriting of functions

Rewrite:

    • When a subclass appears with a function that is identical to the parent class,
    • When a subclass object calls the function, the contents of the subclass function are run.

Coverage:

    • The subclass overrides the parent class, and must guarantee that the child class permission is greater than or equal to the parent class permission before it can be overwritten or the compilation fails.
    • Static can only be overridden by static.

3. polymorphic

Multi-State premise:

    • Must be a relationship between a class and a class, or inherit, or implement
    • Presence Overlay

4. Final keyword

    • Classes, functions, and variables can be decorated.
    • A class that is final decorated cannot be inherited. In order to avoid being inherited, quilt-like replication function.
    • The final modified method cannot be replicated.
    • A final modified variable is a constant that can be assigned only once, with the ability to modify member variables and to modify local variables.

When describing things, the value of some data is fixed, then in order to enhance the readability of the values are given a name. Easy to read.
This value does not need to be changed, so the final modification is added. As constants: The written specification of constants all uppercase letters, if made up of multiple words, between the words through _ connection.

    • The inner class definition is at the local location in the class, and only local variables that are partially final decorated are accessible.

  

interface, inheritance, polymorphism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.