Home >Java >javaTutorial >Detailed explanation of the problem of creating parent class objects in Java inheritance
It is true that the parent class constructor is called, but the parent class object is not created at all, but the parent class constructor is called to initialize the properties.
It is really nonsense to say that calling the parent class constructor method is equivalent to creating a parent class object.
The new instruction opens up space, which is used to store various attribute references of objects, etc. If you decompile the bytecode, you will find that there is only one new instruction, so a space is opened, and one object is placed in each space.
Then, the subclass calls the properties, methods, etc. of the parent class, which is not an instantiated object.
In the bytecode, the subclass will have a u2 type parent class index, which belongs to the CONSTANT_Class_info type. CONSTANT_Utf8_info can be found through the description of CONSTANT_Class_info, and then the specified parent class can be found.
In your method, the attribute names are parsed on this, and then the actual variable content is stored in the space created by new. . .
The super keyword only accesses the data in a specific part of this space (that is, the memory part dedicated to storing parent class data). . . . . .
If a subclass can be forcibly converted into a parent class for use, it is because the Java virtual machine has the concepts 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 when using it, while the static type of the 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 runtime. The compiler does not know what the actual type of the variable is when compiling
Memory of java objects The layout 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 already been determined.
Memory layout of java objects in Hotspot:
Each java object consists of an object header and an object body in memory.
Rule 1: Any object is processed at a granularity of 8 bytes aligned.
Rule 2: Instance fields are arranged according to the following priority: long and double types; integer and floating point types; characters and short integer types; byte types and Boolean types, and finally reference types. These instance fields are aligned according to their respective units.
Rule 3: Instance fields in different class inheritance relationships cannot be mixed. First, the instance fields in the parent class are processed according to Rule 2, followed by the instance fields of the subclass.
Rule 4: If the distance between the last member of the parent class and the first member of the subclass is less than 4 bytes, it must be extended to the basic unit of 4 bytes.
Rule 5: If the first instance field of the subclass is a double or long integer, and the parent class has not used up 8 bytes, the JVM will break Rule 2, according to integer (int), short integer (short), byte type (byte), reference type (reference) order, filling the unfilled space.
The above are the rules for the memory layout of java objects.
Next, let’s talk about the instantiation method of java objects, which is the common
When we create a new object, the jvm has actually allocated the entire space of the object, and the instance domain layout of the entire object has been determined.
The instantiation method
The
Let’s use an example to illustrate:
class Parent {
private short six;
private int age;
}
class Sub extend Parent{
private String name;
private int age;
private float price;
}
The above is the detailed content of Detailed explanation of the problem of creating parent class objects in Java inheritance. For more information, please follow other related articles on the PHP Chinese website!