① What is polymorphic?
Polymorphism (polymorphism) is characterized by its ability to behave in many forms. The same implementation interface that uses different instances to perform different operations
② What is the upward transformation? What are the grammatical requirements?
1, the reference of a parent class to a subclass object, called upward transformation (upcasting), at this time through the parent class reference variable call method is a child class overrides or inherit the parent class method, not the parent class method, at this time through the parent class reference variable cannot call subclass specific method
< parent type > < reference variable > = new < subtype > ()
Automatic type conversion
③ what is the downward transformation? What are the grammatical requirements?
A reference to a subclass that refers to a parent class reference to a subclass object, called a downward Transformation (downcasting), can access a method specific to a subclass. Must be converted to the real subclass type pointed to by the parent class, or a type conversion exception will occur classcastexception
Forcing type conversions
What are the three conditions for ④ to achieve polymorphism?
1, the existence of inheritance (inheritance is the basis of polymorphism, no inheritance there is no polymorphism)
2, subclasses overriding the parent class method (polymorphic call subclasses after overriding the method)
3. Parent class reference variable points to subclass object (subclass to parent class conversion)
⑤ What are the benefits of using polymorphism?
Reducing the amount of code in a class can improve code scalability and maintainability
What is the function and syntax format of the instanceof operator?
Determines whether an object belongs to a class or implements an interface
Boolean result = Object instanceof Class (interface)
Note: When using the instanceof operator, the object must be of the same type as the class or interface specified by the second parameter of Instanceof, or a compilation error will occur, and instanceof is typically used in conjunction with coercion type conversions
The polymorphism of Java