On whether to create parent class object in Java inheritance

one

It is true to call the parent class constructor, but the parent class object is not created at all. It is just to call the parent class constructor to initialize the property.

It is nonsense to say that calling the parent class constructor is equal to creating the parent class object.

The new instruction opens up a space for storing various generic / sexual references of the object. Decompile the bytecode and you will find that there is only one new instruction, so it opens up a space and puts one object in each space.

Then, the subclass calls the properties and methods of the parent class, which is not an instantiated object.

There will be a U2 type parent class index in the bytecode subclass, which belongs to constant_ Class_ Info type, via constant_ Class_ Info description can be found in constant_ Utf8_ Info, and then you can find the specified parent class.

The attribute names of your methods are resolved on this, and then the actual variable contents are stored in the space from New...

The super keyword only accesses the data of a specific part of the space (that is, the memory part dedicated to storing the parent data)......

The default hashcode and equals (directly used = = comparison) are the same, so there is no separate parent object in the same space.

If a subclass can be forcibly converted to a parent class for use, it is because the Java virtual machine has the concept of static type (appearance type) and actual type.

For example, object t = new point (2,3);

Then object belongs to static type (appearance type), and point belongs to actual type.

Both static type and actual type can change in the program. The difference is that the change of static type only occurs during use, while the static type of variable itself will not change, and the final static type is known during compilation; The change result of the actual variable type can only be determined during operation. The compiler does not know what the actual type of the variable is when compiling.

two

The memory layout of Java objects is determined by the class to which the object belongs. It can also be said that when a class is loaded into the virtual machine, the layout of the objects created by this class has been determined.

Memory layout of Java objects in hotspot:

Each Java object consists of an object header and an object body in memory.

The object header is the meta information of the object, including the reference of the class to which the object belongs, as well as some information of hashcode and monitor.

The object body mainly stores the instance domain of the Java object itself and the instance domain inherited from the parent class, and the internal layout meets the following rules:

Rule 1: any object is aligned at a granularity of 8 bytes.

Rule 2: the instance fields are arranged according to the following priorities: long integer type and double precision type; Integer and floating point; Characters and short integers; Byte type and boolean type, and finally reference type. These instance fields are aligned in their respective units.

Rule 3: instance domains in different class inheritance relationships cannot be mixed. First, handle the instance domain in the parent class according to rule 2, and then the instance domain of the child class.

Rule 4: if the interval between the last member of the parent class and the first member of the child class is less than 4 bytes, it must be extended to the basic unit of 4 bytes.

Rule 5: if the first instance domain of the subclass is a double or long integer, and the parent class does not use up 8 bytes, the JVM will break rule 2 and fill the unfilled space in the order of integer (int), short (short), byte (byte) and reference type (Reference).

These are the rules for the memory layout of Java objects.

Next, let's talk about the instantiation method of Java objects, that is, the common < init > method.

When we create a new object, in fact, the JVM has allocated the whole space of the object, and the instance domain layout of the whole object has been determined.

The instantiation method < init > is to set the value of the object instance field to the corresponding space.

The < init > method starts with calling the < init > method of the parent class and ends with its own construction method. The positional relationship between the instance domain declaration and the instance initialization statement block will affect the bytecode order of the < init > method generated by the compiler.

Let's take an example to illustrate:

The memory layout of the current sub object consists of the following:

What does super mean by the so-called representation of parent storage space?

I think the super storage here is the green one!

The above article on whether to create a parent object in Java inheritance is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>