Home > Java > javaTutorial > body text

How is java polymorphism implemented?

(*-*)浩
Release: 2019-12-03 14:12:48
Original
4156 people have browsed it

In Object-Oriented Programming (OOP), the polymorphic mechanism is undoubtedly its most distinctive feature. It can even be said that programming without the use of polymorphism cannot be called OOP.

How is java polymorphism implemented?

Essentially, there are two types of polymorphism:

Compile-time polymorphism (also known as static polymorphism), Runtime polymorphism (also known as dynamic polymorphism) (Recommended learning: java course)

Overload (overload) is an example of compile-time polymorphism. Compile-time polymorphism occurs during compilation. It has been determined at runtime, 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.

The following is a brief introduction to the mechanism of runtime polymorphism (hereinafter referred to as polymorphism).

Polymorphism usually has two implementation methods:

Subclass inherits parent class (extends)

Class implements interface (implements)

No matter which method is used, the core lies in the rewriting of the parent class method or the implementation of the interface method 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. For example, assuming we want to create an ArrayList object, the declaration should use such a statement:

List list=newArrayList();
Copy after login

instead of

ArrayList list=newArrayList();
Copy after login

When defining method parameters, the parent class should always be used first. Type or interface type, for example, a method should be written as:

publicvoid doSomething(List list);
Copy after login

instead of

publicvoid doSomething(ArrayList list);
Copy after login

The biggest advantage of such a declaration is the flexibility of the structure: If one day I think that the characteristics of ArrayList cannot meet my needs Requirements, I hope to use LinkedList to replace it, then I only need to change new ArrayList() to new LinkedList where the object is created, and no other code needs to be changed.

The above is the detailed content of How is java polymorphism implemented?. 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