Home  >  Article  >  Java  >  What is the polymorphism mechanism of java

What is the polymorphism mechanism of java

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-12-27 13:30:113433browse

What is the polymorphism mechanism of java

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 free

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

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
Previous article:What is API in Java?Next article:What is API in Java?