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.

Lets see it with an example
Example1.java

1
2
3
4
5
6
7
8
9
10
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();
    }
    
 
}
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
2
3
4
5
6
class child extends Example1{
    public child() {
        System.out.println("hellow child constrcutor");
        
    }
}
class child extends Example1{
	public child() {
		System.out.println("hellow child constrcutor");
		
	}
}

So what would be the output?

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
2
3
4
5
6
7
8
9
10
11
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();
 
    }
 
}
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
2
3
4
5
class Child1 extends Example2{
    public Child1() {
        System.out.println("inside Child1 constructor");
    }
}
class Child1 extends Example2{
	public Child1() {
		System.out.println("inside Child1 constructor");
	}
}

Child2.java

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

Whats the o/p

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

1
2
3
4
5
6
7
8
9
10
11
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();
 
    }
 
}
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
2
3
4
5
6
7
8
9
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");
    }
}
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
2
3
4
5
class Child2 extends Child1{
    public Child2() {
        System.out.println("inside Child2 constructor");
    }
}
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
2
3
4
5
6
7
8
9
10
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();
    }
 
}
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
2
3
4
5
6
7
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");
    }
    
}
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 will be the output ?

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
2
3
4
5
6
public class Example3 {
    public Example3() {
             this();
        System.out.println("inside Example3 constructor");
    }
}
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

Karibasappa G C (KB)
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