0

Code:

Parent Class:  
public class Parent {  
    int i = 10;    
    {    
        System.out.println(i);  
    } 
    public Parent(){    
        System.out.println("Parent");  
    }  
}

Child Class:  
public class Child extends Parent {  
    int j = 20;  
    {  
        System.out.println(j);  
    }  
    public Child() {  
        super();  
    }  
}  

Test Class:  
public class ConstructorTest {  
    public static void main(String... args){  
        Child c = new Child();  
    }  
}

Execution Flow(What I know):

  1. Identification of instance members from top to bottom in Parent Class.
  2. Identification of instance members from top to bottom in Child Class.
  3. Execution of instance variable assignments and instance blocks from top to bottom in Parent Class.
  4. Execution of Parent constructor.
  5. Execution of instance variable assignments and instance blocks from top to bottom in Child class.
  6. Execution of Child constructor.

Output:

10  
Parent  
20  

My question is: in step 6- During execution of Child constructor, why the parent constructor is not called again using super() of child?
so why the output is not:

10  
Parent  
20  
Parent
Utsav
  • 5,572
  • 2
  • 29
  • 43
  • `Parent`'s constructor is called **once** because you instanciated **one** object. There's no reason it would be called twice. – Spotted Nov 14 '16 at 09:40
  • Utsav as @Spotted say the parent constructor is called as part of the construction of the child object. No need to be called twice – alainlompo Nov 14 '16 at 09:45
  • From your steps, you have provided the 6th point which is the first call happening in the code. 1. Main called 2. There is a new child object created and child constructor called hence super() is called. 3. Super will call the parent class, instance objects hence "10" is printed. 4. Parent class constructor is invoked hence "parent" is printed. 5. Then the child class instance object "20" is printed. 6. End of the Main call. Hope this will clarify your confusion. – Gowthami Reddy Nov 14 '16 at 09:48

2 Answers2

7

The super() line has already taken effect in step 4, basically. Just super(); is implicit and would usually be removed. Typically you use explicit constructor chaining for:

  • Chaining to a constructor in the same class
  • Providing arguments for a constructor

Despite its position as the first line of the body of the child constructor, a this() / super() statement is executed before the instance initializers. You should think of it as being somewhat separate from the rest of the body of the constructor.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

Why should the parent constructor be called more than once? You are creating only one instance of your child class and this invokes the parent constructor:

new Child() -> Child.super() -> Parent()

Thus the string "Parent" is written to the console only once. The other actions are not part of the respective classes' constructor.

dpr
  • 10,591
  • 3
  • 41
  • 71