1. Polymorphism
Encapsulation is an operation to protect a property, and inheritance is to extend the functionality of a class
Polymorphism is another feature of the object-oriented programming language that is derived from encapsulation and inheritance.
Polymorphic manifestation: Overloading and rewriting of methods
1.1 Static polymorphism (compile-time polymorphism): implemented by method overloading (overload)
1.2 Dynamic polymorphism (run-time polymorphism): Subclass objects can be used as parent class objects, with method overrides (Overridder) implementations
2. The polymorphism of the object
There are two main types of polymorphism of objects:
1. Upward Transformation:
Parent class Parent Class object = subclass instance;//program auto-complete
Note the usage rules:
The parent class object is manipulated , so the newly added members (properties and methods) of the child class cannot be accessed.
Methods overridden in subclasses and member variables with the same name
A. member variables (including static variables) and static member methods: Compile and run look to the left (see parent class)
B. Non-static member method: Compile to see the left side, run to the right (the subclass overrides the method).
2. Downward transition:
Subclass Subclass Object = (subclass) Parent class object;//need to cast
Java-Object-oriented article (supplemental)