#java Object-oriented programming has three major characteristics: encapsulation, inheritance, and polymorphism.
Encapsulation hides the internal implementation mechanism of the class. It can change the internal structure of the class without affecting its use, and also protects the data. Only its internal details are hidden from the outside world, and only its access methods are exposed to the outside world.
Inheritance is to reuse parent class code. Inheritance can be used if there is an IS-A relationship between two classes. , and inheritance also pave the way for the realization of polymorphism. So what is polymorphism? What is the implementation mechanism of polymorphism? Please watch me reveal it to you one by one:
The so-called polymorphism means that the specific type pointed to by the reference variable defined in the program and the method call issued through the reference variable are not determined during programming, but It is determined during the running of the program, that is, which class instance object a reference variable will point to, and the method call issued by the reference variable is a method implemented in which class, which must be determined during the running of the program. Because the specific class is determined only when the program is running, the reference variable can be bound to various class implementations without modifying the source program code, causing the specific method called by the reference to change accordingly, that is, it does not need to be modified. The program code can change the specific code bound to the program when it is running, allowing the program to select multiple running states. This is polymorphism.
The above is the detailed content of How to understand java polymorphism. For more information, please follow other related articles on the PHP Chinese website!