Java Object-oriented polymorphism (polymorphism)
-
Polymorphism is constructed on the basis of encapsulation and inheritance; polymorphism (also known as dynamic binding, late binding, or runtime bind run-time binding)
-
The subclass is the parent (the Rose is the flower, the man is the person), so the polymorphic means: the reference of the parent type can point to the object of the subclass;
-
The parent class is the child class is wrong (flower is the rose, the person is the man is wrong, the other flower does not approve, the woman also does not agree)
-
People are often plagued by other non-object-oriented features in the Java language, such as method overloading, which are sometimes thought of as object-oriented features. But don't be fooled: if it's not late binding, it's not polymorphic.
-
Method overloading is early binding, during compilation, Java knows its parameters and types at compile time, and polymorphism is late binding, that is, when running to know, and compile time is not known.
-
Polymorphism separates the interface and the implementation from another angle by separating "what" and "how". Polymorphism not only improves the organization and readability of the code, but also creates "extensible" programs that can be expanded either when the project is initially created or when new features need to be added.
-
Encapsulation creates a new data type by merging features and behaviors. Implement hiding separates the interface and implementation through the details "private". This type of organizational mechanism is easier to understand for those who have a procedural programming background. The role of polymorphism is to eliminate the coupling relationship between types. We already know that inheritance allows the object to be treated as its own type or its base type. This ability is extremely important because it enables multiple types (derived from the same base class) to be treated as the same type, and the same code can run on top of these different types without distinction. Polymorphic method calls allow one type to exhibit the difference from other similar types as long as they are derived from the same base class. This distinction is expressed by the different behavior of the method, although these methods can be called by the same base class.
Java records -14-object-oriented polymorphism