Java creates a subclass object that is called by the constructor of the parent class will not create a parent class

Source: Internet
Author: User
Tags instance method

1. 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 does not have a 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 construction method, because the constructor method is relative to the Java source program, and the compiled class file is the initialization method namely the "<init>" Method (red part is the method name), the initialization method is composed of three parts of the Java source program , a section is a 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;//Initializes the first part//following curly braces for initialization of the second part {this.i=4;//do something ...}}


The third part is the Java source code in the construction method in the code, Java source code has a few construction methods, then the class file has a few initialization methods, the compiler will be the first part and the second part is copied to the front end of each initialization method, It then copies the code of the constructor of the initialization method corresponding to 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 call another initialization method, this initialization method may be one of its own class initialization method, For example, the first sentence in your constructor has a similar this (...) Such a statement, the initialization method invokes the specified constructor of its own class, and if you do not specify a constructor call in your constructor, the initialization method defaults to calling the parent class parameterless initialization method, if the first sentence of your subclass is super (...), The initialization method then 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.

Java creates a subclass object that is called by the constructor of the parent class will not create a parent class

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.