Transformation and memory allocation in Java inheritance, and transformation and allocation in java inheritance

Source: Internet
Author: User

Transformation and memory allocation in Java inheritance, and transformation and allocation in java inheritance

When reading a book, a piece of code can be messy. The code is like this:

Package inheritance; abstract class People {public String tag = "Crazy Java handout"; // ① public String name = "Parent"; String getName () {return name ;}} class Student extends People {// defines a private tag instance variable to hide the tag instance variable of the parent class String tag = "lightweight Java EE Enterprise Application practices "; // ② public String name = "Student";} public class HideTest2 {public static void main (String [] args) {Student d = new Student (); // After the d variable is explicitly converted to Parent, you can access the tag instance variable // the program will output the "crazy Java handout" System. out. println (People) d ). tag); // ④ System. out. println (d. getName (); // parent }}

Running result:

Crazy Java Handouts
Parent

In this Code, the abstract parent class People defines two variables and a getName () method. The subclass student also defines two variables with the same name as the parent class and hides the parent class.

Two confusions about this code: 1. During subclass instantiation, the parent class object must be instantiated first, while the parent class is an abstract class and cannot have objects. Then, when the subclass is instantiated, no parent class object is generated ???

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 variables defined in people, the other is the instance variables defined in Student, and the d variable references a Student object. The memory is as follows:

Convert d to a Parent object in the upward direction. It is allowed to access the name variable through it, that is, to output "parent ".

But looking at his explanation, I still don't understand it. I did not know it clearly. I searched the internet again:

 

Whether a parent class object exists during java subclass instantiation. Questioner: luoyuehao89 | views: 602
Whether a parent class object exists at the same time during java subclass instantiation. suppose that there is an int A = 1 in the parent class a; subclass B inherits A, and B overwrites int a = 2; run: A test = new B (); system. out. println (test. a); the result is 1, which is an attribute in the parent class. whether a parent class object exists at this time. In my understanding, it exists. I tried again to modify the parent class with abstract. It is reasonable to say that abstract is not instantiated. It is certainly not possible to obtain the attribute in the parent class. The result is the same. how to understand.

Question added:

Is it true that when a subclass object is created, a parent class object will certainly appear?
Wonderful answers
No parent class object will be generated, but only the constructor of the parent class will be used. The constructor will generate an object instead of the constructor. The constructor will only initialize the object, instead of generating the object, if new A (); that is, only the new statement will generate the object of the parent Class. Variable is static binding, and the method is dynamic binding. During the compilation, the variables bind the variable CALL statement to the variable definition assignment statement. Instead, they are bound to the parent class because the type is parent class during the call, so the value is defined in the parent class. In fact, you can understand that when you create a subclass object, there are two variables in the memory of the subclass object, one inherited from the parent class, A subclass. No parent class object is generated. The members of the parent class are inherited to the subclass object, and the parent class member is called by referencing the parent class pointing to the subclass object, it is just to find the inherited parent class member from the sub-class object memory space, that is, to call variable a with the sub-class object, so that it can explain that the member must call through the object, however, in this case, a sub-class object is called a that inherits from its parent class (two sub-class objects, one inherited from its parent class and the other belongs to its own class. I am confused about this problem for a long time. I found many people are wrong during online queries. I finally found several good articles to understand it, many java veterans may also make the "generate parent class Object" error, which was recently understood. Think for yourself, if a parent class object is generated, if the parent class is an abstract class, can an abstract class generate an object? This statement is not rigorous.
Dynamic binding definition 
Dynamic binding is used to determine the actual type of the referenced object during execution (not during compilation) and call the corresponding method based on the actual type.
Static binding and dynamic binding
In addition to restricted access, the access method also determines which method calls the quilt class or which attribute accesses the quilt class. the association between function calls and functions and the relationship between member access and variable memory addresses is called binding.
There are two main binding methods in computer languages: static binding and dynamic binding. static binding occurs between the data structure and the data structure, before the program is executed. static binding occurs during the compilation period, so you cannot use any runtime information.
It targets the body of function calls and functions, or blocks in variables and memory .. dynamic binding only uses available information during the runtime for access requests generated during the runtime. in object-oriented code, dynamic binding means deciding which method is called or which attribute is accessed,
It is based on this class rather than the access range.

Who has a better explanation? Please leave a message .. Thanks
Well explained: http://bbs.csdn.net/topics/390896785
Explanation:
After a subclass creates an instance, the class initialization method calls the initialization method of the parent class (except for java. lang. object Class, Because java. lang. object class does not have a parent class), and this call will be traced step by step until java. lang. object initialization method.
I am talking about the initialization method, not the constructor, because the constructor is relative to the java source program, the compiled class file is the initialization method, that is, the "<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 private int I = 0; private Date date = new Date (); and so on. The second part is composed of initialization blocks, for example:

Java code
Public class Test {
Private int I = 0; // initialize the first part
// Initialize the second part in the following braces
{This. I = 4; // do something ......}}
The third part is the code in the constructor in the java source code. There are several constructor methods in the java source code, so there are several initialization methods in the class file, the compiler copies the first and second parts to the front end of each initialization method, and then initializes
The code of the constructor of the parameter corresponding to the method is copied to the corresponding initialization method (the copy mentioned here is actually a compilation, but so it is said for your better understanding ).
So how to record the parent class of the initialization method is also related to the structure of the initialization method. The execution sequence and structure of the initialization method are as described above, however, the first execution command of each initialization method is to call another initialization method,
This initialization method may be an initialization method of your own class. For example, the first sentence in your constructor is similar to this (...) the initialization method calls the specified constructor of the class. If the constructor is not specified in your constructor,
The initialization method will call the non-parameter initialization method of the parent class by default. If the first sentence of your subclass is super (...), the initialization method will call the parent class to specify the initialization method. This call process will be called recursively until this class is a java. lang. Object Class.
Calling the initialization method does not mean that an object will be generated. If the new Keyword and the constructor call appear in your java code, only one object will be generated, and its parent class object will not be generated, therefore, it is reasonable to call the constructor that the parent class is an abstract class.
The initialization method is just a common method named "<init>" for virtual machines, the difference is that it is called after an object is generated. (sun's jdk private package has a way to generate an object by bypassing the constructor. It can prove that the above statement is not stated here ).
Then, answer your second question. The constructor in the abstract class is actually used for the inherited subclass, because the constructor is equivalent to the initialization method, when a subclass calls a constructor, the parent class constructor must be called,
So you can initialize fields in the abstract class and execute some initialization code as needed when the subclass generates objects. In fact, it is not necessary to generate an instance of a class to call the constructor. Subclass also needs to call the parent class constructor.
The constructor is not always called to generate an instance. In some special implementations or special circumstances, the constructor is not called to generate an instance. A constructor does not necessarily generate an instance, but it must be called by an instance, just like an ordinary instance method.

 

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.