Transformation in Java inheritance and its memory allocation

Source: Internet
Author: User

Reading the time by a piece of code can be messy, the code is this:

 Packageinheritance;Abstract classpeople { PublicString tag = "Crazy java handout";//①         PublicString name = "Parent"; String GetName () {returnname; }            }    classStudentextendspeople {//define a private tag instance variable to hide the tag instance variable of the parent classString tag = "Lightweight Java EE Enterprise Application Combat";//②         PublicString name = "Student"; }     Public classHideTest2 { Public Static voidMain (string[] args) {Student d=NewStudent (); //You can access the tag instance variable by explicitly transforming the D variable up to the parent//The program will output: "Crazy java Handout"System.out.println (((People) d). tag);//④System.out.println (D.getname ());//Parent        }    }

Operation Result:

Crazy Java Handouts
Parent

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

Two puzzles about this code:1. When a subclass is instantiated, the parent class object must be instantiated first, and the parent class is an abstract class and cannot have an object. That to the base class instantiation of the production does not produce the parent object???

2.d.getname ();//The parent is returned, not student. Should not be hidden from the Father class?

This is how it is explained in the book:

The student object holds two instance variables, one is the instance variable defined in the people, one is the instance variable defined in the student, the D variable refers to a student object, and the memory is as follows:

The D-up is converted to a parent object, where it is allowed to access the name variable, which is the output "parent".

But looking at his explanation is still a little unclear, said is not very clear, and went to the Internet search:

whether a parent class object exists at the same time when the Java subclass is instantiated. 2011-10-14 19:53 questioner: luoyuehao89 | PageView: 602 times
Whether a parent class object exists at the same time when the Java subclass is instantiated. If the parent class A has an int a = 1, subclass B inherits A, and b covers an int a = 2; run: a test = new B (); System.out.println (test.a); The result is 1, which is the parent class The attributes in the. This time there is a parent object, and my understanding is there. I try again, the parent class with abstract abstraction, supposedly abstract tired can not instantiate it, certainly can't get the parent Class A property, the result is the same. How to understand.

Supplementary questions:

Is it possible to create a subclass object that will definitely appear with a parent class object?
Wonderful Answer
does not produce the parent class object, only uses the parent class constructor, does not use the constructor to produce the object, the constructor is only the object initialization function, but does not produce the object function, if new A (); That is, only the new statement will produce the object of the parent class A. A variable is a static binding, by dynamic binding. In this case, the variables in the compiler implement the variable call statement and variable definition assignment statement binding, the nature of the binding is the parent class, because the type is the parent class, so the value is a value defined in the parent class in fact, you can understand that  when you create a subclass object, in the subclass object memory, there are two copies of this variable, A copy is inherited from the parent class, a sub-class. The parent class object is never produced, the member of the parent class is inherited to the subclass object, the parent class member is invoked with the parent class reference to the child class object, except that the inherited parent class member is found from the subclass object memory space, that is, the object is called by the subclass objects. This can explain the rules that a member must call through an object, except that a child class object that is called inherits from the parent class A (there are two A in the subclass object, one inherits from the parent class, one belongs to itself) hey, the words are a bit messy.  This problem also puzzled me for a long time, the Internet query found a lot of people are wrong, finally found a few good articles to understand, perhaps a lot of Java veteran will also make "produce parent object" This error, only recently understand. Think of yourself, if the parent class object is generated, if the parent class is an abstract class, does the abstract class allow the object to be generated? So this is not a rigorous statement.
Dynamic binding Definitions
 Dynamic binding is the actual type of the referenced object that is judged during execution (non-compile time), and its corresponding method is called according to its actual type.
static binding and dynamic binding
  

It is called for function calls with the body of the function, or a variable with a chunk in memory: Dynamic binding is the access request generated for the runtime, using only the information available for the run time. In object-oriented code, dynamic binding means deciding which method is called or which property is accessed.
will be based on the class itself and not on the access scope.

Who has a better explanation, say more clearly, welcome message. Thanks
This explanation is good: http://bbs.csdn.net/topics/390896785
The explanations are as follows:
Subclasses after the instance is created, the class initialization method calls the parent class's initialization method (except for the Java.lang.Object class, because the Java.lang.Object class has no parent Class), and the call is traced back to the Java.lang.Object initialization method.
This place I'm talking about is the initialization method, not the constructor method, because the constructor method is relative to the Java source program, and the compiled class file is the initialization method, the "<init>" Method (the red part is the method name),
The initialization method is composed 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 consists of an initialization block, for example:

Java Code
public class test{
private int i=0;//Initialization of the first part
The following curly braces are initialized in the second part of the
{this.i=4;//do something ...} }
The third part is the Java source code in the construction method in the code, Java source code, there are several construction methods, then the class file has a few initialization methods, the compiler will copy the first part and the second part to the front end of each initialization method, and then the initialization
The code of the method corresponding to the method of constructing the parameter is copied into the corresponding initialization method (the copy in this case is actually compiled, but in order to give you a better understanding so to speak).
So how the initialization method traces its parent class, which is also related to the structure of the initialization method, the execution order of the initialization method and the structure as above, but the first execution instruction of each initialization method is to invoke another initialization method,
This initialization method may be an initialization method of its own class, such as the first sentence in your constructor has a similar this (...) Such a statement, the initialization method invokes the specified construction method of its own class, and if you do not specify a constructor method call in your constructor method,
Then the initialization method calls the parent class parameterless initialization method by default, and if the first sentence of your subclass is super (...), the initialization method invokes the parent class to specify the initialization method. This invocation is called recursively until the class is the Java.lang.Object class.
Invoking the initialization method does not imply that the object will be generated, and that the new keyword in your Java code, along with the call to the constructor, produces only one object whose parent object is not generated, so it is perfectly reasonable to call the parent class as the constructor of the abstract class.
And the initialization method for the virtual machine is just a common method named "<init>", the difference is only the generation of objects later called (Sun's JDK private package has a way to bypass the construction method to generate objects, can prove above the statement, specifically how I do not state).
And then answer your second question, the constructor in the abstract class is actually used for the inherited subclass, because the constructor method is equivalent to the initialization method, and when the subclass calls the constructor method, the parent class constructor method must be called.
So you can initialize the fields in the abstract class as required in the abstract class and execute some initialization code when the subclass produces the object. In fact, it is not necessary to generate an instance of a class to call the constructor method, and the subclass needs to call the parent class construction method.
The build instance does not necessarily call the constructor method, and in some special implementations or special cases, the build instance does not call the constructor method. Calling a constructor method does not necessarily generate an instance, but it must be called by an instance, just like a normal instance method.

Transformation in Java inheritance and its memory allocation

Related Article

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.