Rajinder Menu

Constructor Chaining in Java



Constructor chaining means calling one constructor of a class from another constructor of that class or calling superclass constructor from subclass constructor either implicitly or explicitly.

Thus constructor chaining happens at two scenarios:

1) Within the class i.e. calling one constructor of this class from within the another constructor of this class.

For this check my previous article here[Link].

2) Between superclass and its subclasses

It is described in this article.

Constructor chaining between superclass and subclass is somewhat related to multilevel inheritance.

Multilevel Inheritance



It happens either implicitly or explicitly.


Implicit constructor chaining

Implicit constructor chaining use the concept of implicit calling of constructors. It happens with the help of no argument constructor present in superclass and an automatic call to this no argument constructor in superclass provided by compiler in subclass.

For example

class ParentA{
  
  int num;

  ParentA(){
    this.num=10;
    System.out.println("In ParentA constructor.");
  }
}

class ParentB extends ParentA{
  int num1;

  ParentB(){
    this.num1=10;
    System.out.println("In ParentB constructor.");
  }
}

public class Child extends ParentB{
  int i;

  Child(int i){
    this.i=i;
    System.out.println("In Child constructor.");
  }

  public static void main(String[] args) {

   Child childObject= new Child(13);

  }
}

Output:

In ParentA constructor.
In ParentB constructor.
In Child constructor.

In above code class Child extends class ParentB while class ParentB extends class ParentA. So this is multilevel inheritance.

Compiler provided an automatic call to no-argument constructor of ParentB in constructor of Child class as:

Child(int i){

  ParentB(); //compiler provided call
  this.i=i;
  System.out.println("In Child constructor.");
}

Similarly compiler provided a call to no argument constructor of ParentA in the constructor of ParentB as:

ParentB(){

  ParentA(); //compiler provided call
  this.num1=10;
  System.out.println("In ParentB constructor.");

}

Therefore the output is:

In ParentA constructor.
In ParentB constructor.
In Child constructor.

For above compiler provided calls to no argument constructor work it is necessary that superclasses must have a no argument constructor provided in them otherwise compiler will give error.


This is known as implicit constructor chaining between superclass and its subclasses.



Implicit constructor chaining and default constructor

If no other constructor is provided in a class i.e. neither no argument nor parametrized, compiler provides a constructor which is known as default constructor which has no arguments and its body is empty.

In this case subclass will call superclass default constructor by compiler provided call but the superclass default constructor will do nothing because its body is empty.

For example

class ParentA{
  
  int num;

  //no constructor is present neither no-arg nor parametrized
}

class ParentB extends ParentA{
  
  int num1;

  ParentB(){
    this.num1=10;
    System.out.println("In ParentB constructor.");
  }
}

  public class Child extends ParentB{
    int i;
    Child(int i){
    this.i=i;
    System.out.println("In Child constructor.");
  }

  public static void main(String[] args) {

    Child childObject= new Child(13);

  }
}

Output:

In ParentB constructor.
In Child constructor.

In above code class ParentA has no user defined constructor so compiler will provide a default constructor but it will do nothing hence no output.


Explicit constructor chaining

Explicit constructor chaining is achieved using super keyword. Here we do not depend on compiler provided call to no argument constructor but we explicitly call superclass constructor in subclass using super.

Explicit calling is for the scenario where class do not have no argument constructor but have parameterized constructors. So here compiler provided call to no argument constructor does not work.

For example

class ParentA{

  int num;

  //parametrized constructor. No no argument constructor is present
  ParentA(int num){
    this.num=num;
    System.out.println("In ParentA constructor.");
  }
}

class ParentB extends ParentA{

  int num1;

  //parametrized constructor. No no argument constructor is present
  ParentB(int num1){
    this.num1=num1;
    System.out.println("In ParentB constructor.");
  }
}

public class Child extends ParentB{

  int i;

  Child(int i){
    this.i=i;
    System.out.println("In Child constructor.");
  }

  public static void main(String[] args) {

   Child childObject= new Child(13);

  }
}

If you run above code it will compile time error:

Implicit super constructor ParentA is undefined. Must provide explicit call to another constructor.

Implicit super constructor ParentB is undefined. Must provide explicit call to another constructor.

For above code to work we have to provide explicit call to superclass constructors of class ParentA and ParentB as:

class ParentA{

  int num;

  ParentA(int num){
    this.num=num;
    System.out.println("In ParentA constructor.");
  }
}

class ParentB extends ParentA{

  int num1;

  ParentB(int num1){
    super(10); //explicit call to superclass constructor
    this.num1=num1;
    System.out.println("In ParentB constructor.");
  }
}

public class Child extends ParentB{

  int i;

  Child(int i){
     super(13); //explicit call to superclass constructor
     this.i=i;
     System.out.println("In Child constructor.");
  }

  public static void main(String[] args) {

   Child childObject= new Child(13);
  
  }
}

Output:

In ParentA constructor.
In ParentB constructor.
In Child constructor.

This is known as explicit constructor chaining which use the concept of explicit constructor calling.

I would like to know your comments and if you liked the article then please share it on social networking buttons.


No comments:

Post a Comment