Polymorphism is reflected in the code:
The reference to the parent class or interface to the object of its child class.
Benefits of Polymorphism:
Increases the extensibility of the code, which can be used later by the code defined in the previous section.
Disadvantages of polymorphism:
Pre-defined content cannot use (invoke) the specific content of the late sub-class.
Prerequisites for polymorphism:
1, must have relations, inheritance, implementation.
2, to have coverage.
Automatic type promotion, cat objects raise the animal type. However, unique features cannot be accessed by S.
The role is to restrict access to unique features.
Professional speaking: upward transformation. Hides the subtype. You do not have to use the unique methods of subclasses.
If you want to use specific animal cat features.
You can transform the object down.
Cat C = (cat) a;//downward transformation is intended to use unique methods in subclasses.
C.eat ();
C.catchmouse ();
Note: For transformation, it has always been a subclass object that has changed in type.
Animal A1 = new Dog ();
Cat C1 = (cat) a1;//classcastexception
if (a instanceof Cat)//instanceof: Used to determine the specific type of object. Can only be used for reference data type judgments
It is often used for robustness judgments before downward transformation.
Polymorphic,
Features of the Members:
1, member variable.
Compile time: Refer to whether the referenced variable belongs to a class that has a calling member variable, have, compile through, no, compile failed.
Runtime: Refer to whether the referenced variable belongs to a class that has a calling member variable, and run the member variable in the owning class.
Simply put: both compile and run refer to the left side of the equals sign. Oh, yes.
As an understanding.
2, member function (non-static).
Compile-time: refers to whether there are functions called in the class to which the referenced variable belongs. There, compile through, no, compile failed.
Runtime: Refers to whether there are functions called in the class to which the object belongs.
Simply put: Compile to look to the left and run to see the right.
Because the member function has an override attribute.
3, static function.
Compile-time: refer to whether there are static methods called in the class to which the referenced variable belongs.
Runtime: Refers to whether there are static methods called in the class to which the referenced variable belongs.
Simply put, compile and run all look to the left.
In fact, for static methods, the object is not required. It can be called directly with the class name.
The polymorphism of Java