Parent class and sub-class Constructor

Source: Internet
Author: User

This evening, the instructor asked me a question about the subclass and parent class constructor. Let's make an analysis and summary:

 

RunCode:

 

 
Package left-rotated string; public class A {public void testabstract () {system. out. print ("parent method"); system. out. println ("I =" + I);} A () {system. out. println ("Palace 1"); testabstract ();} public static void main (string [] ARGs) {CC cc = new CC (); system. out. println ("Palace 2"); CC. testabstract () ;}} class CC extends a {private int I = 1; Public void testabstract () {system. out. print ("subclass method"); system. out. println ("I =" + I);} CC (){}}

 
Result: Palace 1 subclass method I = 0 Palace 2 subclass method I = 1 cc = new CC ();

This code is an object that instantiates the CC class and will be called to the CC constructor.

 

The execution of the CC constructor is like this process:

1. at first, the constructor of the parent class is called implicitly to initialize non-static fields of the parent class. When the constructor of the parent class A is executed, it also calls the constructor of the parent class, that is, the object class Constructor (the constructor is called to the upper-level at a layer, objects at the top layer are returned to the constructor of CC ).

2. Call testabstract () in the constructor of parent class A. Is this method of parent class or subclass? Multiple instances are involved here. testabstract () is equivalent to this. testabstract (); this indicates the current object. Although this method is called in the parent class constructor, the parent class constructor is called by the quilt class, this indicates the current object of CC, not the parent class object. Therefore, testabstract () called in the parent class is the override method of the subclass. The running result proves this.

 

Why does the testabstract () method of the subclass also call different results printed in place1 and place2?

This is related to the variable initialization problem of the constructor. Before the constructor is executed, the system has applied for memory for the entire object, and the member variables of the object have been applied for memory, but Initialization is still pending, therefore, the system assigns 0 to the default int value. When you enter the constructor, the first thing is to call the constructor of the parent class. At this time, I has not been assigned a value, and its value is still 0, the subclass method is called in the parent class constructor, so the output value is 0;

In place2, execute the command again.TestabstrAct () indicates the expected result. 1. The Initialization is complete.

 

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.