Polymorphism: Multiple forms of an object (inheritance is the basis for polymorphic implementations)
⑴ Reference polymorphism: A reference to a parent class can point to an object of this class; A reference to a parent class can point to an object of a subclass;
⑵ method Polymorphism: When creating this type of object, the method called is the method of this class; When a subclass object is created, the method that is called is the method that the subclass overrides or the inherited method;
⑶ a reference to a parent class cannot call a method that is unique to a subclass.
The two most critical benefits of polymorphism are: polymorphic parameters and heterogeneous sets;
The keyword instanceof is used to determine whether an object is an instance of a particular class.
Abstract class:
Abstraction is the ability to write abstract classes, which are classes that cannot be instantiated, and the keyword abstract is used to declare a class as an abstract class.
- Syntax definition: The abstract keyword is used to decorate before the class.
- Scenario: ① In some cases, a parent class simply knows what the subclass should contain, but does not know exactly how these subclasses implement these methods; ② abstracts an abstract class from multiple classes with the same characteristics, taking this abstract class as a template of subclasses, thus avoiding the arbitrariness of sub-class design;
- Role: Restriction subclasses must implement certain methods, but do not pay attention to implementation details;
- Using rules: ①abstract defines abstract classes, ② defined abstract methods, only declarations, no implementations, ③ classes containing abstract methods are abstract classes, ④ abstract classes can contain ordinary methods, or there can be no abstract methods; ⑤ Abstract classes cannot be created directly, you can define reference variables to point to child class objects.
Java polymorphic and abstract classes, interfaces