Constructor Chain

As we all know that Constructor in java always executes when we create an object.

But what is the order of execution of constructors when we have parent child relationship ?

Always constructor’s default first statement is super(); which means it calls super class constructor.

Let’s see it with an example


Example1.java

  1. package com.kb.constructorchain;
  2.  
  3. public class Example1 {
  4.     public Example1() {
  5.         System.out.println("hellow parent constrcutor");
  6.     }
  7.    
  8.    
  9.     public static void main(String[] args) {
  10.         child c = new child();
  11.     }
  12.    
  13.  
  14. }
package com.kb.constructorchain;

public class Example1 {
	public Example1() {
		System.out.println("hellow parent constrcutor");
	}
	
	
	public static void main(String[] args) {
		child c = new child();
	}
	

}


Child.java

  1. class child extends Example1{
  2.     public child() {
  3.         System.out.println("hellow child constrcutor");
  4.        
  5.     }
  6. }
class child extends Example1{
	public child() {
		System.out.println("hellow child constrcutor");
		
	}
}


As soon as we reach the execution to child c = new child(); statement , child() constructor is called.

since default first statement inside child() constructor is super(), it executes parent class constructor and control comes back to second line of child’s constructor and execution continues.

Example2.java

  1. package com.kb.constructorchain;
  2.  
  3. public class Example2 {
  4.    
  5.     public Example2() {
  6.     System.out.println("inside Example2 constructor");
  7.     }
  8.  
  9.     public static void main(String[] args) {
  10.        
  11.         Child2 c = new Child2();
  12.  
  13.     }
  14.  
  15. }
package com.kb.constructorchain;

public class Example2 {
	
	public Example2() {
	System.out.println("inside Example2 constructor");
	}

	public static void main(String[] args) {
		
		Child2 c = new Child2();

	}

}


Child1.java

  1. class Child1 extends Example2{
  2.     public Child1() {
  3.         System.out.println("inside Child1 constructor");
  4.     }
  5. }
class Child1 extends Example2{
	public Child1() {
		System.out.println("inside Child1 constructor");
	}
}


Child2.java

  1. class Child2 extends Child1{
  2.     public Child2() {
  3.         System.out.println("inside Child2 constructor");
  4.     }
  5. }
class Child2 extends Child1{
	public Child2() {
		System.out.println("inside Child2 constructor");
	}
}


What if we call explicitly subclass constructor from the same subclass constructor ?

  1. package com.kb.constructorchain;
  2.  
  3. public class Example2 {
  4.    
  5.     public Example2() {
  6.     System.out.println("inside Example2 constructor");
  7.     }
  8.  
  9.     public static void main(String[] args) {
  10.        
  11.         Child2 c = new Child2();
  12.  
  13.     }
  14.  
  15. }
package com.kb.constructorchain;

public class Example2 {
	
	public Example2() {
	System.out.println("inside Example2 constructor");
	}

	public static void main(String[] args) {
		
		Child2 c = new Child2();

	}

}


Child1.java

  1. class Child1 extends Example2{
  2.     public Child1() {
  3.         this(8);
  4.         System.out.println("inside Child1 constructor");
  5.     }
  6.    
  7.     public Child1(int x) {
  8.         System.out.println("inside Child1 parameterized "
  9.                 + "constructor");
  10.     }
  11. }
class Child1 extends Example2{
	public Child1() {
		this(8);
		System.out.println("inside Child1 constructor");
	}
	
	public Child1(int x) {
		System.out.println("inside Child1 parameterized "
				+ "constructor");
	}
}


Child2.java

  1. class Child2 extends Child1{
  2.     public Child2() {
  3.         System.out.println("inside Child2 constructor");
  4.     }
  5. }
class Child2 extends Child1{
	public Child2() {
		System.out.println("inside Child2 constructor");
	}
}


Child2() -> super-> child1() -> child1(int) -> super -> Example2 -> child1(int) executes -> child1() continues -> child2() continues


Example3.java

  1. package com.kb.constructorchain;
  2.  
  3. public class Example3 {
  4.     public Example3() {
  5.         System.out.println("inside Example3 constructor");
  6.     }
  7.    
  8.     public static void main(String[] args) {
  9.    
  10.         Child c = new Child();
  11.     }
  12.  
  13. }
package com.kb.constructorchain;

public class Example3 {
	public Example3() {
		System.out.println("inside Example3 constructor");
	}
	
	public static void main(String[] args) {
	
		Child c = new Child();
	}

}


Child.java

  1. class Child extends Example3{
  2.     public Child() {
  3.         this(10);
  4.         System.out.println("inside Child zero arg constructor");
  5.     }
  6.     public Child(int x) {
  7.         System.out.println("inside Child's parameterized constructor");
  8.     }
  9.    
  10. }
class Child extends Example3{
	public Child() {
		this(10);
		System.out.println("inside Child zero arg constructor");
	}
	public Child(int x) {
		System.out.println("inside Child's parameterized constructor");
	}
	
}


What happens if in the above program I replace

  1.  Child c = new Child();
 Child c = new Child();

By

  1.  Child c = new Child(10);
 Child c = new Child(10);


Note :
If the subclass constructor does not specify which super class constructor to invoke then the compiler will automatically call the accessible no-args constructor in the super class. No matter whether subclass constructor has ‘this’ call or not, super call will always be there internally.


Constructor Ground Rules

1) Recursive constructor call is not allowed.

Example, below program will not compile

  1. public class Example3 {
  2.     public Example3() {
  3.              this();
  4.         System.out.println("inside Example3 constructor");
  5.     }
  6. }
public class Example3 {
	public Example3() {
             this();
		System.out.println("inside Example3 constructor");
	}
}


2) Always constructor first statement should be ‘this’ or ‘super’ but never be both.

‘this’ calls same class constructor and ‘super’ calls immediate parent constructor.

Notable points about constructor in Java

1) Default constructor is added automatically for any class in java

2) If there is any parameterized constructor exist then default constructor is not added automatically.

3) super() call to super class default constructor is the default first statement of any constructor 
     if there is no 'this' or 'super' call explicitely.

4) Both ‘super’ and ‘this’ can never be used for single constructor, any one of them is allowed.

5) ‘super’ and ‘this’ should be the first statement inside a constructor.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on