Java Foundation-subclass inherits parent class instantiation object procedure

Source: Internet
Author: User

Before encountering such a problem in the project:

Class B inherits the class, a, and when it is instantiated, a method of B is called in the constructor of a, and the member property of B is initialized in the method of B, and then the member property of the B object is empty.

The code scenario is as follows:

  

When B New comes out, S will be "s = null" overrides the value of the call to Init in a, and if the method called in A is overridden by a quilt class, the A-to-image method is a child-object method, but a pair of like-to-call methods are the methods of the sub-objects, and then the code block of B is

In order to S=null overwrite the original initialization value, we can remove the initialization of s in the code block.

Let's take a detailed analysis of the code execution process during the construction of the subclass.

Legend:

  

This is a parent-child class model with static code blocks, initialization code blocks, constructor methods, and member methods, so the process of constructing TT2 () is as follows:

New TT2 ()->new TT1 (), static tt1 1-> static tt1 2-> tt1list = new ...-> tt1 constructor->tt2 static TT2 1-> tt2 static Tt2 2->list = null-> tt2 contructor->end

This may not be too clear, we use the graph to mark the following:

The output results are as follows:

  

Summary: Tired loading process no matter how complex, always follow-static code block---Common code block---construction method, the order of execution, in order to follow the step-by-step analysis, the structure of the program can be smoothed clear to avoid unnecessary errors.

PS: Internal class invocation of external class properties-internal classes when using external classes, when constructing an inner class, an external class is automatically added to the inner class, and the inner class is the method in the external class that is called through the reference.

Java Foundation-subclass inherits parent class instantiation object procedure

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.