Polymorphic
Parent class Type parent class reference =new subclass type ();
Polymorphic three-factor
1 Inheritance relations
2 to have a method rewrite
When a subclass does not override a parent class method, the child class object calls the method of the parent class
When the parent class has no methods, the parent class object cannot be used to invoke compilation without passing
When compiling, what type of object to read, and what type to access
At run time, the corresponding method is invoked according to the actual type passed in
JVM dynamic binding mechanism to invoke the appropriate method based on the actual type of runtime
3 parent class reference to child class object
Abstract methods
1. Abstract methods not specifically implemented
Access modifier abstract return type method name ();
2. The class in which the abstract method resides must be an abstract class
Access modifier abstract class class name {}
3. Subclasses must implement abstract methods in the abstract parent class, unless the subclasses themselves are abstract
4. Abstract classes can have abstract methods, or there can be no
5. Abstract classes cannot be instantiated
Upward transformation and upward transformation
Upward Transformation: The parent class's reference points to the subclass object. Automatic type conversion
Transition up:< Parent type > < reference variable name > = new < subtype > ();
The method that is called by the parent class reference variable is a subclass that overrides or inherits the parent class's method, not the parent class's method
You cannot call a method that is unique to a subclass by referring to a variable from the parent class
Downward transformation: Assigns a reference to a subclass object to the parent class reference to a child class.
That is, the parent class type is converted to a subclass type. Forced type conversion required
Subclass Type sub-class reference name = (subclass type) parent class reference;
In the process of downward transformation, a type conversion exception occurs if there is no conversion to a real subclass type
The instanceof operator is provided in Java to determine the type
if (parent class references instanceof subclass type) {
(subclass type) Subclass reference = (subclass type) parent class reference;
Subclass references. Subclass-specific methods ();
}
Parent class type As the return value of a method
Access modifier parent class type method name (parameter) {
return specific subclass object;
}
Jen Baby Java Basic notes polymorphism