In what form are Java parent and child classes associated in memory?

The easiest way to understand is to first instantiate the parent class object when creating the object, then instantiate the child class object, and assign the parent class object reference as the attribute of the child class object to the child class object. And when this property name is simply understood as super (implicit object composition?)

When creating an object with a subclass of the parent class, will two objects be created in memory, one pointing to the subclass and the other pointing to the parent class?


To test this assumption, the following is an understanding based on <In-depth understanding of the java virtual machine>

For developers who only need to apply this feature, this explanation is relatively simple

and what does it actually look like?

This inheritance relationship can be explained from two aspects:

one is the inheritance relationship

of classes, the other is how this relationship is implemented in memory when creating subclass objects


1. Inheritance relationship
of , when it comes to the structure of the Class class file, the structure contains the inheritance relationship mentioned here

this_class
super_class
interfaces

The super_class here stores the parent class of the class, so the jvm hard-coded to implement this inheritance feature. The full name of the parent class can be found through super_class


Field table collection: The field table collection will not list fields inherited from the superclass or parent interface , but may list fields that do not exist in the original java code, such as In order to maintain access to the outer class in the inner class, a reference to the outer class instance is automatically added. The p178


method table collection: corresponding to the field table collection, if the parent class method is not overridden in the subclass, the method table The method information from the parent class will not appear in the collection





2. How is this inheritance relationship reflected in the object? (Implicit combination?)
As mentioned in <In-depth understanding of java virtual machine>, objects are divided into 3 areas in memory (heap): object header (Header), instance data (Instance Data) and alignment padding (Padding)

objects In the header (Header) related to the inheritance relationship is the type pointer, through which the pointer of the class metadata is found . When using the instanceof operator, it can be judged whether it is an instance of the parent class. I think it is judged by the inheritance relationship super_class of the class, rather than by the object to reflect the parent-child relationship.


Instance Data (Instance Data)
<In-depth understanding of java virtual machine As mentioned in >, whether it is inherited from the parent class or defined in the subclass, it needs to be recorded in the instance data module.
There are two explanations here:
one is that a copy of the parent class is created.
It can be understood that when a subclass object is created, the variables of the object in the parent class will also create an identical copy in the subclass . After these two variables are created, There should be no relationship, and they do not affect each other (in line with the assumption of the above implicit combination).

The second explanation: these variables are the variables of the parent class, and the variables pointed to by super are these variables (the object instance of the parent class is not created)

. For information obtained from other articles or forums, the second explanation is preferred here.
How to interpret the super. method name, find the method through super_class in the class file structure?


What is this super not explicitly mentioned in <In-depth understanding of the java virtual machine>?




Reference:
In java, when creating a subclass object, will the parent class object be created together?
https://www.zhihu.com/question/51920553

The inheritance principle of java
http://blog.csdn.net/tangtang5156/article/details/44218919

Understand the order of class initialization and instantiation of inheritance relationship from the principle:
https ://my.oschina.net/xpbug/blog/111371

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327040126&siteId=291194637