Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the characteristics of java polymorphic mechanism?

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces you to java polymorphism mechanism principle characteristics is what, the content is very detailed, interested friends can refer to reference, hope to help everyone.

What is Java polymorphism?

The mechanism for polymorphism in java is to rely on references to parent classes or interfaces to point to child classes. Thus, the characteristics of multiple forms of an object are realized. A reference to a parent class dynamically points to a specific instance when the program is running. When calling the method of the reference, it does 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 the instance object of which class a reference variable points to, and the method call issued by the reference variable is the method implemented in which class, which must be determined during the execution of the program.

Because the specific class is determined at runtime, in this way, without modifying the source code, reference variables can be bound to various class implementations, resulting in the specific method called by the reference changing accordingly, that is, without modifying the program code, the specific code bound at runtime can be changed, so that the program can choose multiple running states. This is polymorphism.

Features:

A parent class reference to a child class can only access methods and properties owned by the parent class because it is cast upward, and a reference to a method that exists in the child class but does not exist in the parent class cannot be used, even if the method is overloaded.

If a subclass overrides some methods in its parent class, those methods must be invoked using those methods defined in the subclass (dynamic linking, dynamic invocation).

Knowledge Point Extension

Implementation Principle of Polymorphism

Java object method calls are implemented by relying on the method table in the class information.

In general, when a method of an object is invoked, the JVM looks up the method table of the object class to determine the direct reference address of the method, and only then does it actually invoke the method.

A superclass inherits a method from its parent class, and if it is not overridden, it is invoked to point to the parent class's method. If Overrding the method, point to the code section of the class.

However, superclasses have method tables for their parent classes.

About java polymorphism mechanism principle characteristics is what to share here, hope the above content can have some help to everyone, can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report