The implementation mechanism of Java polymorphism is that reference variables defined by the parent class or interface can point to instance objects of the subclass or implementation class, and the methods called by the program are dynamically bound at runtime. , refers to the method of the specific implementation object pointed to by the reference variable, that is, the method of the object running in the memory, rather than the method defined in the type of the reference variable.
Essentially, there are two types of polymorphism:
·Compile-time polymorphism (also known as static polymorphism)
·Run-time polymorphism (also known as dynamic polymorphism)
Overload is an example of compile-time polymorphism. Compile-time polymorphism is determined at compile time, and the determined method is called at runtime.
What we usually call polymorphism refers to runtime polymorphism, that is, it is not sure which specific method to call at compile time, and it is delayed until runtime. This is why polymorphic methods are sometimes called deferred methods.
Polymorphism usually has two implementation methods:
## ·Subclass inherits parent class (extends)
·Class implements interfaces (implements)
No matter which method it is, its core lies in the rewriting of the parent class method or the interface Implementation of methods to achieve different execution effects at runtime. To use polymorphism, you should follow one rule when declaring objects: always declare the parent class type or interface type, and create the actual type. PHP Chinese website has a large number of freeJAVA introductory tutorials, everyone is welcome to learn!
The above is the detailed content of What is the polymorphism mechanism of java. For more information, please follow other related articles on the PHP Chinese website!