Polymorphism is the most important concept in object-oriented.
Polymorphism occurs when an object, compile-time type and run-time type are inconsistent. Or, when an object, using the same method, has a different running state, indicating that there is polymorphism.
Two types of polymorphism:
Overloading and overriding of methods
Object polymorphism (can be applied directly on abstract classes and interfaces)
Related concepts:
There are two types of reference variables in Java: Compile-time type and run-time type.
A compile-time type is determined by the type used when declaring the variable, and the run-time type is determined by the object that is actually assigned to the variable.
Object Polymorphism in Java, the object of a subclass can be used instead of the object of the parent class.
A variable can only be determined by a type of verse
A reference variable may point to (reference) many different types of objects.
Subclasses can be thought of as special parent classes, so a reference to a subclass type can point to the object of a subclass: transform up.
If a reference-type variable is declared as the type of the parent class, but the child-class object is actually referenced, the variable cannot be added to the properties and methods in the access subclass.
The property is determined at compile time, so the subclass is the runtime type and the added variable does not exist.
method is dynamically bound, and the method's invocation is determined at run time.
Multi-State implementation
There is a need for inheritance or implementation relationships.
To have an overwrite operation.
Member method, compile time: To see if the class to which the reference variable belongs is yet to invoke the method.
Runtime: Invokes the overridden method in the class that actually belongs.
Member variables are not polymorphic, only the class to which the reference variable belongs.
instanceof Test A variable is a return Boolean for an object of XX
The two classes required to be compared must be a relationship between the subclass and the parent class, or there will be a compilation error.
x instanceof A If x belongs to sub-class B x instanceof a value of a is also true
Java Personal Learning Note: polymorphic