Home > Java > javaTutorial > body text

What is the polymorphism mechanism of java

爱喝马黛茶的安东尼
Release: 2019-12-27 13:30:11
Original
3620 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template