Home >Java >Javagetting Started >What is java polymorphism mechanism

What is java polymorphism mechanism

angryTom
angryTomOriginal
2020-02-03 14:25:202624browse

What is java polymorphism mechanism

What is the polymorphic mechanism in java

The mechanism to achieve polymorphism in java relies on the reference of the parent class or interface to point to the child kind. Thus realizing the characteristics of multiple forms of an object. The reference of the parent class dynamically points to a specific instance when the program is running. When the method of the reference is called, it is not run according to the method defined in the type of the reference variable, but according to the method of the specific instance.

Concept

Polymorphism refers to which class instance object a reference variable will point to, and which class the method call issued by the reference variable is. The methods implemented in must be determined while the program is running.

Because the specific class is determined when the program is running, the reference variable can be bound to various class implementations without modifying the source program code, resulting in the specific method called by the reference changing. This change means that the specific code bound to the program when it is running can be changed without modifying the program code, allowing the program to select multiple running states. This is polymorphism.

Features:

The parent class reference pointing to the subclass has been transformed upward. It can only access the methods and properties owned by the parent class, and for the subclass If a method exists but does not exist in the parent class, the reference cannot be used, even if the method is overloaded.

If the subclass overrides some methods in the parent class, when calling these methods, the methods defined in the subclass must be used (dynamic connection, dynamic calling).

(Related video tutorial sharing: java video tutorial)

The above is the detailed content of What is java polymorphism mechanism. 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