How to Understand the polymorphism in Java

Source: Internet
Author: User

I. Basic Concepts

Polymorphism: Send a message to an object so that the object determines the action to respond.
You can call a dynamic method by assigning a subclass object reference to a variable referenced by a superclass object.
This mechanism of Java follows the principle that when a super-Class Object references a variable to reference a subclass object, the type of the referenced object rather than the type of the referenced variable determines who calls the member method, however, the called method must have been defined in the superclass, that is, the method covered by the quilt class.
1. If a is a reference of Class A, A can point to an instance of class A or a subclass of Class.
2. If a is a reference of interface a, a must point to an instance of a class that implements interface.

II. Implementation Mechanism of Java Polymorphism

Sun's current JVM implementation mechanism, class instance reference is to point to a handle pointer, this handle is a pair of pointers:
A pointer points to a table. In fact, this table also has two pointers (one pointer points to a method table containing objects, and the other points to class objects, indicating the type of the object );
The other Pointer Points to the memory space allocated from the Java heap.

Iii. Summary

1. assign a subclass object reference value to a superclass object to reference a variable to call a dynamic method.
Derivedc C2 = new derivedc ();
Baseclass a1 = c2; // baseclass base class. derivedc is a subclass inherited from baseclass.
A1.play (); // play () is defined in baseclass and derivedc. This method is overwritten by the subclass.
Analysis:
* Why can an object instance of the subclass type be overwritten with a superclass reference?
Automatic upward transformation. With this statement, the compiler automatically moves the subclass instance up to become a general type baseclass;
* A. Play () will execute subclass or parent class definition methods?
Subclass. During runtime, the corresponding method is obtained based on the actual type referenced by the object. So there is polymorphism. Objects of a base class are referenced by different subclass objects. Different actions are performed when this method is executed.
When a1 = c2, there are still two handles, A1 and C2, but A1 and C2 have the same data memory block and different function tables.
2. You cannot reference a parent class object to a subclass object to reference a variable.
Baseclass a2 = new baseclass ();
Derivedc C1 = a2; // Error
In Java, upward transformation is automatically performed, but downward transformation is not. We need to define it as mandatory.
C1 = (derivedc) A2; forced conversion, that is, downward transformation.
3. Remember a simple and complex rule. A type reference can only reference methods and variables contained in the reference type.
You may say that this rule is incorrect, because when the parent class references a subclass object, the method of the subclass is executed.
In fact, this is not a conflict, because later binding is adopted, and the subclass method is called according to the type during dynamic operation. If the subclass method is not defined in the parent class, an error occurs.
For example, in addition to inheriting the functions defined in baseclass, The derivedc class also adds several functions (such as myfun ())
Analysis:
When you use the parent class to reference and point to a subclass, the JVM has actually used the type information generated by the compiler to adjust and convert the type information.
As you can understand, it is equivalent to setting functions not contained in the parent class to invisible from the virtual function table. Note that some function addresses in the virtual function table may have been rewritten in the subclass, therefore, the virtual function Project address in the object virtual function table has been set as the address of the method body completed in the subclass.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.