Home  >  Article  >  Java  >  How to understand java polymorphism

How to understand java polymorphism

silencement
silencementOriginal
2019-05-29 17:54:062926browse

How to understand java polymorphism

#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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn