java polymorphism

java polymorphism


Java has three necessary conditions for polymorphism:
1. Inheritance, in polymorphism, there must be subclasses and superclasses with inheritance relationship.
2. Overriding, the subclass redefines some methods in the parent class, and when these methods are called, the methods of the subclass are called.
3. Upward transformation, in polymorphism, it is necessary to assign the reference of the subclass (the pointer to the object of the subclass) to the object of the parent class (the pointer of the object of the subclass is transformed into the type of the parent class), only in this way can the reference have the skills to call the parent class. methods and methods of subclasses. (Without this, the method you call will always be of the subclass)


Understand (explain in parentheses) the priority of method calls:
1. When the superclass (parent) object reference variable refers to the subclass object (that is, the superclass type) (object reference variable) points to (reference) subclass object), the type of the referenced object (subclass object) rather than the type of the reference variable (pointer) determines whose member method is called.
2. The called method must It is defined in the superclass, that is to say, the method overridden by the subclass (overridden by the subclass), but it still confirms the method according to the priority of the method call in the inheritance chain, which is: this.show( O), super.show(O), this.show((super)O), super.show((super)O).

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326982772&siteId=291194637