If Java creates a subclass object, it will call the constructor of the parent class. Will it create a parent class?

Source: Internet
Author: User

1. 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 sourceProgramThe compiled class file is the initialization method, that is, 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 = new date (); and so on. The second part is composed of the initialization block, for example:

Java code
Public class test {private int I = 0; // initialize the first part. // the second part is initialized in the following braces {This. I = 4; // do something ......}}


The third part is Java Source code In the constructor Code There are several constructor methods in the Java source code, so 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, then, copy the code of the constructor of the parameter corresponding to the initialization method to the corresponding initialization method (the replication mentioned here should actually be compiled, But it is said so 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, which may be an initialization method of its 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 calls 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, it must call the constructor of the parent class. Therefore, you can initialize fields in the abstract class as required when the subclass generates an object and execute some initialization code. 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.

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.