Transformation and memory allocation in Java inheritance

When reading a book, it can be messy by a piece of code. The code is as follows:

@H_ 301_ 7 @ operation results:

Crazy Java handout parent

In this code, the abstract parent class people defines two variables and a getname () method, and the child class student also defines two variables with the same name as the parent class to hide the name of the parent class.

Two puzzles about this Code: 1 When instantiating a subclass, you must first instantiate the parent class object, and the parent class is an abstract class and cannot have an object. Will the parent object be generated when the underlying subclass is instantiated???

2.d. getName();// Parent is returned instead of student Shouldn't the parent class be hidden??

The book explains this:

The student object will save two instance variables, one is the instance variable defined in people, the other is the instance variable defined in student, and the D variable refers to a student object. The memory diagram is as follows:

Transform D upward into a parent object. It is allowed to access the name variable through it, that is, output "parent".

But I still didn't understand his explanation. I didn't say it very clearly. I searched the Internet again:

Whether there is a parent object when instantiating a Java subclass

If there is an inta = 1 in parent class A;

Subclass B inherits from a, and B covers multiple inta = 2;

function:

Atest=newB();

system. out. println(test.a);

The result is 1, which is an attribute in the parent class My understanding is whether there is a parent object at this time

I also try to modify the parent class with abstract abstract. It is reasonable that abstract cannot be instantiated. It is certain that the a attribute in the parent class cannot be obtained, and the result is the same

How to understand

Supplementary questions:

Is it certain that a parent class object will appear when a child class object is created?

Wonderful answer

No parent class object will be generated, but the constructor of the parent class is used. The object will not be generated if the constructor is used. The constructor is only used to initialize the object, not to generate the object. If newa(); that is, only the new statement will generate the object of parent class A.

Variables are statically bound and methods are dynamically bound. During compilation, variables implement the binding of variable call statements and variable definition assignment statements. The bound variables are naturally of the parent class. Because the type is of the parent class when calling, the value is the value defined in the parent class

In fact, you can understand that when you create a subclass object, there are two copies of this variable in the memory of the subclass object, one inherited from the parent class and one part of the class.

The parent class object will never be generated. The members in the parent class are inherited into the child class object. Calling the parent class member with the parent class reference pointing to the child class object is just to find the inherited parent class member from the memory space of the child class object, that is, it is essentially calling variable a with the child class object, which can explain the provisions that members must be called through the object, But at this time, the a inherited from the parent class in the subclass object is called (there are two a in the subclass object, one inherits from the parent class and the other belongs to itself)

Hey, it's a bit confusing. This problem has puzzled me for a long time. I found that many people are wrong when I query the Internet. Finally, I found a few good articles to understand. Maybe many Java veterans will also make the error of "generating parent objects", which has only been understood recently.

Think for yourself, if the parent class object is generated, and if the parent class is an abstract class, is the abstract class allowed to generate objects? So this statement is not rigorous

@H_ 301_ 7 @ dynamic binding definition

Dynamic binding refers to determining the actual type of the referenced object during execution (non compilation time) and calling its corresponding method according to its actual type

@H_ 301_ 7 @ static binding and dynamic binding

In addition to restricting access, the access method also determines which method will be called by the subclass or which property will be accessed by the subclass The association between function call and function itself, as well as the relationship between member access and variable memory address, is called binding

There are two main binding methods in computer language, static binding and dynamic binding Static binding occurs between data structures and before program execution Static binding occurs at compile time, so no runtime information can be used

It targets function calls and function bodies, or variables and blocks in memory Dynamic binding only uses the available information of runtime for the access requests generated during runtime In object-oriented code, dynamic binding means deciding which method is called or which property is accessed,

@H_ 301_ 7 @ will be based on the class itself, not on the access scope

After the subclass creates an instance, the class initialization method will call the initialization method of the parent class (except for the java.lang.object class, because the java.lang.object class has no parent class), and this call will be traced level by level until Java Initialization method of lang.Object.

I'm talking about the initialization method here, not the construction method, because the construction method is relative to the Java source program, and the compiled class file is the initialization method, that is "< init >" method (the red part is the method name),

The initialization method consists of three parts of the Java source program. One part is the direct initialization statement after the member field, such as privateinti = 0; privateDatedate=newDate(); The second part is composed of initialization blocks, for example:

Javacode

publicclassTest{

privateinti=0;// Initialize part I

//The second part of initialization is in the following braces

{this.i=4;//dosomething......}}

The third part is the code in the construction method in the Java source code. If there are several construction methods in the Java source code, there are several initialization methods in the class file. The compiler will copy the first part and the second part to the front end of each initialization method respectively, and then initialize

The code of the constructor of the corresponding parameter of the method is copied into the corresponding initialization method (the copy here should actually be said to be compilation, but in order to make you better understand it, so it is said)

So how the initialization method traces its parent class is also related to the structure of the initialization method, the execution order and structure of the initialization method. As mentioned above, but the first execution instruction of each initialization method is to call another initialization method,

This initialization method may be an initialization method of its own class. For example, the first sentence in your constructor is similar to this (...) This statement, the initialization method will call the specified constructor of its own class; If no constructor call is specified in your constructor,

Then the initialization method will call the parent class parameterless initialization method by default. If the first sentence of your subclass is super (...), Then the initialization method will call the initialization method specified by the parent class. This calling process will call recursively until the class is Java Lang.Object class.

Calling the initialization method does not mean that an object will be generated. The new keyword in your Java code and the call of the construction method will only generate an object, and its parent object will not be generated. Therefore, it is entirely reasonable to call the construction method whose parent class is an abstract class.

Moreover, the initialization method is just a common method for the virtual machine called "
". The difference is only after the object is generated. It is called later. (sun's JDK private package has the way of bypassing the construction method to generate objects, which can prove the above statement and how I do not specify it here).

Then answer your second question. The constructor in an abstract class is actually used for inherited subclasses, because the constructor is equivalent to an initialization method. When a subclass calls the constructor, it must call the constructor of the parent class,

So you can initialize the fields in the abstract class and execute some initialization code when the subclass generates objects. In fact, it is not necessary to generate an instance of a class before calling the constructor. Subclasses also need to call the constructor of the parent class.

The generated instance does not necessarily call the construction method. In some special implementations or special cases, the generated instance will not call the construction method. Calling the constructor does not necessarily generate an instance, but it must be called by an instance, just like an ordinary instance method.

@H_ 301_ 7 @ summary

The above is all about the transformation and memory allocation in Java inheritance. I hope it will be helpful to you. Interested friends can continue to refer to this site: code examples of inheritance in Java programming Java object-oriented programming (encapsulation / Inheritance / polymorphism) instance parsing, etc. if you have any questions, you can leave a message at any time. Xiaobian will reply to you in time. Thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>