Java transformation in inheritance and Memory Allocation

Source: Internet
Author: User
Whether a parent class object exists during java subclass instantiation. 2011-10-14
19: 53

Questioner: luoyuehao89 | browsing times: 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?

Let me help.

   




Report | 2011-10-22
16: 33

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.
 
 
 
 
 
 
Package jc.cn; public class Car {public int a; public int B;} class Truck extends Car {byte c; byte d ;} class Smp {public static void main (String [] args) {Truck t = new Truck () ;}} after the following statement is executed: Truck t = new Truck (); evaluate the memory model of an object?
Let me help.
   

Report |

Satisfactory answer

Stack space is on the left, heap space is on the right, and the variable t in stack space stores an address pointing to an object in the heap space.

Dynamic binding definition

Dynamic binding is used to determine the actual type of the referenced object during the execution period (not during the compilation period), and call the corresponding method to edit the static binding and dynamic binding of the object according to its actual type.

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 the class and not based on the access scope.

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.