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
- 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
- class child extends Example1{
- public child() {
- System.out.println("hellow child constrcutor");
- }
- }
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
- 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
- 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
- class Child2 extends Child1{
- public Child2() {
- System.out.println("inside Child2 constructor");
- }
- }
class Child2 extends Child1{ public Child2() { System.out.println("inside Child2 constructor"); } }
What if we call explicitly subclass constructor from the same subclass constructor ?
- 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
- 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
- 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
- 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
- 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 happens if in the above program I replace
- Child c = new Child();
Child c = new Child();
By
- 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
- 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.
In previous this operator explanation, you have mentioned this is not used for construction calling..
package com.kb.javainsimpleway;
class Person{
String name;
int age;
public Person() {
this(“javainsimpleway”,20);
}
public Person(String name,int age){
this.name=name;
this.age=age;
}
}
public class ThisKeywordEx2 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);
System.out.println(person.age);
}
}
Then how the examples here, using this operator to call it’s constructor. Isn’t it contradict?
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.}
Which previous post, Please provide the link
awesome explaination!
Great explaination ,, its more than enough about constructor 🙂
Thank for sharing your valuable knowledge
Its my pleasure Bro 🙂
ultimate explained