Constructor Chaining in Java Inheritance | Java Tutorials

This Java Tutorials “Constructor Chaining in Java Inheritance” explains clearly how to connect constructors in Parent Child relation using this() method and super().


 

Constructor Chaining in Parent-Child:

  • If we don’t define any constructor inside the class, compiler supplies a default constructor.
  • Java ‘Default constructor’ is a zero args constructor-Empty body.

 

Note : Compiler will not add any default constructor if we define any constructor inside the class.

 

Note : We need to take care of all the constructor availability in constructor chaining process among Parent-child object creation.

 

Note : In the process of constructor chaining in Parent-Child classes, we need to check all the constructors available or not.

 

  • In the following example, we defined 4 constructors in Parent and Child class.
  • We can connect all the constructors in the instantiation of Child class in 4 ways.
  • Note that, call to either super() or this() must be the first statement in the constructor.
  • That means we can connect only one constructor from another constructor.

Connect constructors in Parent-Child relation

 

 

Share this