Extract
Java polymorphism, how to understand parent class references to child class objects
To understand polymorphism, you first need to know what is "upward transformation".
I defined a subclass cat, which inherits the animal class, and the latter is the parent class. I can pass cat C = new Cat (); Instantiating a cat object is not difficult to understand.
But when I define this: Animal a = new Cat ();
Represents a reference that defines a animal type, pointing to an object of the new cat type. Because Cat is inherited from its parent class animal, references to animal types can point to objects of the cat type.
So what's the point of doing that? Because subclasses are an improvement and an extension to the parent class, the generic subclass is more powerful than the parent class, the property is more unique than the parent class, and the object that defines a reference to a child class of a parent class can use both the powerful functions of the subclass and the commonality of the parent class.
Therefore, a reference to a parent class type can invoke all the properties and methods defined in the parent class, but it is helpless for a method that is not in the parent class defined in the subclass, and a method in the parent class is only defined in the parent class and is not overridden in the subclass. Can be called by a reference to a parent class type, and for a method defined in the parent class, if the method is overridden in a subclass, then a reference to the parent class type invokes this method in the subclass, which is the dynamic join. It can also be called dynamic binding.
Dynamic binding refers to the actual type of object being referenced during execution, not during compilation, and calls its corresponding method according to the actual type.
Look at the following procedure:
Class Father {
public void Func1 ()
{
Func2 ();
}
This is the Func2 () method in the parent class, because the method is overridden in the following subclass, so when called in a reference to the parent class type, the method is no longer valid, instead of the Func2 () method that is overridden in the calling subclass
public void Func2 () {
System.out.println ("AAA");
}
}
Class Child extends Father {//func1 (int i) is an overload of the Func1 () method
Because this method is not defined in the parent class, it cannot be called by a reference of the parent class type
So in the following Main method, CHILD.FUNC1 (68) is wrong.
public void func1 (int i) {
System.out.println ("BBB");
}//Func2 () overrides the Func2 () method in the parent class father if the Func2 () method is called in a reference to the parent class type, then this method must be overridden in the subclass
public void Func2 () {
System.out.println ("CCC");
}
}
public class Polymorphismtest {
public static void Main (string[] args) {
Father child = new Child ();
CHILD.FUNC1 ();//What will the print result be? } }
The above procedure is a very typical polymorphic example. Subclass Child inherits the parent class father and overloads the Func1 () method of the parent class, overriding the Func2 () method of the parent class. The overloaded func1 (int i) and func1 () are no longer the same method, and because there is no func1 (int i) in the parent class, the reference child of the parent class type cannot call the func1 (int i) method. When the subclass overrides the Func2 () method, the reference child of the parent class type calls the method, and the child class overrides the Func2 ()
}
}
So what kind of results will the program print? Obviously, it should be "CCC".
For polymorphism, it can be summed up as:
One, a reference to the child class using the parent class type;
Second, the reference can only invoke methods and variables defined in the parent class;
Thirdly, if a method in the parent class is overridden in a subclass, the method in the subclass is called when the method is called; (dynamic connection, dynamic invocation)
variables cannot be overridden (overwritten), the concept of "overriding" is only for methods, and if a variable in the parent class is "overridden " in a subclass, an error is made at compile time.
3 Prerequisites for Polymorphism:
1. Inheritance 2. Rewrite 3. The parent class reference points to the subclass object.
——————————————————————
Upward transformation: person p = new Man (); No forced type conversions are required for upward transformation
Down transformation: Man man = (man) new person (); Type conversions must be enforced
Java polymorphism, how to understand parent class references to child class objects