Final, abstract class, interface, Polymorphic,

Source: Internet
Author: User
Tags define abstract

final ——— finally. As a modifier

    1. Can modify classes, functions, variables;

    2. A class that is final modified cannot be inherited;

    3. The final modified method cannot be rewritten;

    4. A final modified variable can only be assigned once, either to modify member variables or to modify local variables;

    5. When an inner class defines a local position in a class, only the local variables that are final decorated are accessible.

abstract class -- not much different from the general class, but an abstract function (that is, you can define abstract methods in a class)

    1. Abstract methods must be defined in abstract classes

    2. Abstract methods and abstract classes must be decorated with the abstract keyword

    3. An abstract class cannot create an object with new, because invoking an abstract method makes no sense

    4. The method in the abstract method is to be used, and the subclass object invocation must be established after all its abstract methods have been replicated by the subclass.

      If a subclass overrides only a subset of the abstract methods, then the subclass is an abstract class.

Special: Abstract classes can not be defined in an abstract way, simply do not allow the class to establish an object.

? Template Method Design pattern

When defining a feature, part of the feature is deterministic, but part of it is indeterminate, and the identified part is using the indeterminate part, then exposing the indeterminate part to be done by the subclass of the class.

? Interface

    ? Initial understanding, can be considered a special abstract class

? When a method in an abstract class is abstract, the class can be represented by an interface;

Class is used to define classes? Interface used to define interfaces

? When the interface is defined, the format features:

1. Common definitions in interfaces: constants, abstract methods;

? 2, the members of the interface have fixed modifiers;

?    ?    ? ? constant modifier: public static final

?    ?    ? ? Method: Public abstract

Note: the members in the interface are public;

interface: is not possible to create objects, because there are abstract methods, the need to implement the quilt class, sub-class docking in the mouth of the abstract method is all covered, the subclass can be instantiated, otherwise the subclass is an abstract class.

?    ? The interface can be implemented by many classes.

features of the interface: 1, the interface is the rule of external exposure, 2, the interface is the function of the extension of the program, 3, the interface can be used for multiple implementations, 4, the class is implemented between interfaces, and the class can inherit a class while implementing multiple interfaces. 5, interface and interface can have an inheritance relationship (multiple inheritance)

    ? Polymorphic ? --can be understood as the various manifestations of the existence of things

? 1, the manifestation of polymorphism

? The reference to the parent class points to its own subclass object;

? ? A reference to a parent class can also accept its own subclass object;

? 2, the premise of polymorphism

?    ? ? must be a relationship between classes and classes, either inherited or implemented;

?    ? ? There is usually a premise that there is coverage;

3. The benefits of polymorphism

? ?? The appearance of polymorphism greatly improves the expansibility of the program;

Cons: Improved extensibility, but only the members in the parent class can be accessed using references in the parent class.

? 4, polymorphic applications

?

Note: 1, you can force the reference of the parent class to the subclass type, downward transformation, 2, cannot convert the parent class object to the subclass type, 3, we can convert the parent class to its own subclass object, the application can be promoted, can be cast; 4, Polymorphism to the end is a subclass of objects doing transformations.

? instanceof between reference types

?

? The characteristics of member functions (non-static) in polymorphism:

?    ? ? At compile time: see if there is a method called in the class to which the referenced variable belongs. If there is, compile through, if not, compile failed;

?    ? ? During run time: see if there is a method called in the class to which the object belongs;

the characteristics of the member function (static):

    ?    ? ? either compile or run the reference to the left;

?  ? when a member function is called in a polymorphic state, the compiler looks to the left, runs to the right, and the member variable is either compiled or run, referencing the left side (the class to which the reference variable belongs);

Final, abstract class, interface, Polymorphic,

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.