Java polymorphism from the JVM point of view

Source: Internet
Author: User

First, make it clear that there are three prerequisites for Java polymorphism:

1. Inheritance

2. Subclasses overriding parent class methods

3. Parent class reference to child class object

And then look at an example

 Packagetest.xing;classfather{protected intAge ;  PublicFather () { age= 40; }        voideat () {System.out.println ("Father is eating."); }}classChildextendsfather{protected intAge ;  PublicChild () { age= 18; }        voideat () {System.out.println ("The Child is eating"); }    voidPlay () {System.out.println ("The child is playing CS"); }} Public classtestpolymorphic { Public Static voidMain (string[] args) {Father C=NewChild ();        C.eat (); //C.play ();System.out.println ("Age:" +c.age); }}

The output is:

It is concluded that when the three conditions of the Java polymorphism are full, it is found that the c.eat () call is actually eat of the subclass, but the c.age is called the age of the parent class, and C.play () is not compiled.

The following is an explanation of the above from the JVM's point of view

Let's cut from father C = new Child ()

This sentence first executes new child () and assigns an object to the heap.

Of course, when assigning an instance of the child class, the class file corresponding to the child class is loaded into the JVM through the JVM's ClassLoader, and the JVM produces a struct that represents the type information in the class file based on the byte stream in the class file

The specific implementation of this structure, different JVMs will have different implementations, but almost all the same, to comply with the JVM specifications.

The structure that represents the type information in a class file is probably composed of the following parts:

1, Chang

2. Class variables (static variables)

3. Field information

4. Method information

5, class of the parent class information

6, class of access information, etc.

These I said is not accurate enough, specific people can see the JVM-related books such as "in-depth understanding of Java Virtual Machine", here is a general idea.

After that, the JVM generates something called a method table based on the structure above. This method table is a key to implementing Java polymorphism .

The method table contains direct references to instance methods (those that are accessed with objects relative to static methods), that is, the method table provides access to the instance methods of the class.

Also, these instance methods include not only the methods of this class, but also the instance methods of their parent classes, as well as the instance methods of the parent class (that is, until object).

Also, these methods do not contain private methods (because private methods cannot inherit)

These direct applications in the method table point to the corresponding method information for the struct that represents the type information in the JVM (that is, one of the 4 of the structure above), but this is only a method of this class, and the table also has a method of the parent class, which points to the specific location of the parent class type information structure.

may not be clear enough to express, the following picture shows.

As mentioned above, the method table includes not only the methods of this class, but also the methods of the parent class, which are generated by the method table values, taking the method table of the child class as an example:

In the first method table, a reference to a method that inherits from the object class is generated, including pointing to ToString and pointing to equals, and of course there are many methods in object, which is not written here.

The method table then produces a reference to a method that inherits from the parent class, including Eat,

Finally, a reference to a method of this class is generated.

One thing to note here is that when the method table of the child class produces a reference to a method in the parent class, there is a reference to the Eat method, and finally a reference to the method of this class, and a reference to the Eat, when the new data overwrites the original data. That is to say that the reference to Parent.eat is replaced by a reference to the exponentially to Child.eat ( which occupies the position in the original table ). So we see that the method table in the child class points to child.eat and the parent class's method table points to parent.eat. There is no reference to Parent.eat in the method table of the subclass.

It is also important to note thatin the method table for parent and child, the reference to eat is the same as the offset in the table, which is the third position . ( this is because the subclass Eat method overrides the parent class eat method, occupying the original parent class Eat method's reference in the table position )

To say more, the method information in the struct that represents the class information contains only this class , or the method information that is overridden, without the method information of the parent class .

After understanding the structure of the method area, we look at the structure of the objects in the heap

Next is the stack area, which produces a reference to the father type, which points to an instance of the child class in the heap area.

Here we need to explain the meaning of Father C, we know that C represents a reference to an instance of the child class in the heap, which is plainly an address that points to an instance of the child's class in the heap, but let us not forget that there is a father in front of this C

We all know that there is a void type pointer in C, and limiting the way the pointer accesses memory by restricting a type to the pointer, such as char * p, means that p can only access memory one byte at a byte, but the p in int *p must have four bytes and four bytes to access memory.

But we all know that pointers are unsafe, and one of the unsafe factors is that pointers may access unallocated memory space, which means that char * restricts the way the P pointer accesses memory, but does not limit the amount of memory that can be accessed, which is entirely up to the programmer .

in the Java reference, however, father not only specifies how C accesses memory, but also the size of the memory space that can be accessed .

We see that the size of the father instance object is two rows, but the child instance object occupies three rows (here is a simple quantization).

So while C is pointing to the child instance object, but father it before, it can only access two rows of data , that is, C cannot access the age!! in the child class. ! Can only access the age of the Father class, so output 40

And we note the method table for two classes:

We see that the method table of the parent is three rows, the child's method table takes 4 rows, and C points to an instance object of the child class, and the object also has pointers to the method table of the child class, but because C has been father decorated, Through c You can only access the contents of the first 3 rows in the Child method table !!!!

However, as stated earlier, in the process of forming a method table, the subclasses override the method to overwrite the original data in the table. That is, the third row of the method table for the child class is a reference to child.eat, not to Parent.eat (because the method table produces overrides), so C accesses the child.eat. That is the method of the subclass!!! In this case, C is no way to directly access the Eat method of the parent class .

The above is the interpretation of the output results.

It took about two days to look at the JVM virtual machine, not careful enough to see the flaws in the place. Thank you.

Java polymorphism from the JVM point of view

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.