Chapter 5 Knowledge Points

 1. Through polymorphism, the amount of code in the class can be reduced, and the scalability and maintainability of the code can be improved. Inheritance is the basis of polymorphism, without inheritance there is no polymorphism.

2. Converting a subclass to a parent class is called upcasting, and type conversion is performed automatically. Converting a parent class to a subclass is called downcasting, and a forced type conversion must be performed.

3. The method called through the parent class reference variable after the upward transformation is the method that the child class overwrites or inherits from the parent class. The method specific to the child class cannot be called through the parent class reference variable.

4. After downcasting, you can access the subclass-specific methods, which must be converted to the real subclass type pointed to by the parent class, otherwise a type conversion exception ClassCastException will occur.

5. The instanceof operator is used to determine whether an object belongs to a class or implements an interface

. 6. The instanceof operator is usually used in conjunction with coercive type conversion. Type judgment is performed through instanceof first, and then the corresponding coercive type conversion is performed.

7. Using the parent class as a method parameter and using the parent class as the return value type is a common way to use polymorphism.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325210510&siteId=291194637