Wednesday, August 18, 2021

Constructor Chaining in Java Example - Calling one constructor from another using this and super

Constructor Chaining in Java
In Java, you can call one constructor from another and it’s known as constructor chaining in Java. Don’t confuse between constructor overloading and constructor chaining, the former is just a way to declare more than one constructor in Java. this and the super keyword is used to call one constructor from other in Java. this() can be used to call another constructor of the same class while super() can be used to call a constructor from superclass in Java. Just keep in mind that this() in reality calls the no-argument constructor of the same class while this(2) calls another constructor of the same class which accepts one integer parameter.


Similarly, super() can be used to call the no-argument constructor of super class and super with a parameter can be used to call other overloaded constructors of the parent class. Calling one constructor from another is called constructor chaining in Java, which we saw while discussing constructor overloading in Java

Constructor chaining is also used to implement a telescoping pattern where an object can be created with a combination of multiple properties. 

In our last tutorial, we have seen some important properties of Java constructor as well as answered the question What is Constructor in Java and in this Java tutorial we will see examples of how to call one constructor from another for the same class and superclass.


How to call an overloaded constructor in Java

What is Constructor chaining in Java with exampleConstructor overloading allows you to declare multiple constructors while constructor chaining allows you to call those constructors. This is the main difference between constructor overloading and constructor chaining. 

Since constructor can only be called from another constructor in Java, constructor chaining is an important concept to understand to know How and when a class is initialized in Java.  



Here is a complete code example of constructor chaining which shows How to call an overloaded constructor of the same class and parent class in Java.

/**
 * Simple Java program to demonstrate how to call one constructor from another.
 * Calling one constructor from another is called constructor chaining.
 * this() is used to call the constructor of same class while super() is used to
 * call constructor of Super class in Java.
 *
 * @author Javin  Paul
 */

public class ConstructorChainingExample {
 
 
    public static void main(String args[]) {
   
        //this will first call one argument constructor of Child Class which
        //in turn call corresponding constructor of super class using super(String)
        System.out.println("Constructor chaining Example in Java");
        Child child = new Child("Jeremy");
     
        //this constructor will call no-argument constructor of Child,
        //which then call one argument constructor of
        //same class, which finally call corresponding one argument constructor
        // of super class Parent.
        System.out.println("---------------------------------");
        Child emptyChild = new Child();
    }
 
}

class Parent{
    private String name;
    /*
     * Calling constructor of the same class with one String argument
     */

    protected Parent(){
        this("");
        System.out.println("No argument constructor of Parent called ");
    }
 
    protected Parent(String name){
        this.name = name;
        System.out.println("One String argument constructor of Parent called ");
    }
}

class Child extends Parent{
    private String name;
 
    /*
     * Calling constructor same class with one argument
     */

    protected Child(){
        this("");
        System.out.println("No argument constructor of Child called ");
    }
 
    /*
     * Calling constructor of super class with one argument
     * call to super() must be the first line in the constructor
     */

    protected Child(String name){
        super(name);
        System.out.println("One argument constructor of Super class called from sub class ");
    }
}

Constructor chaining Example in Java
One String argument constructor of Parent called
One argument constructor of Super class called from sub class
---------------------------------
One String argument constructor of Parent called
One argument constructor of Super class called from sub class
No argument constructor of Child called

That’s all on What is constructor chaining in Java. We have seen How to call overloaded constructors from the same class using this() and constructors from superclass using super(). The key thing to remember is that call to another constructor must be the first line in calling a constructor.


Other Java programming tutorials you may find useful

6 comments:

  1. What is the real use of Constructor Chaining ? What happen if I don't know this concept, does it really affect anything practically ?

    ReplyDelete
    Replies
    1. If we have call constructor first default,parameterized at that time it can be use

      Delete
  2. @Shreya- What happen if you don't know java? Knowing something is better than nothing

    ReplyDelete
  3. Hi Javin,

    What is the real need of constructor chaining , i understand variables are initialised which can be re used ,other than this..any other reason?

    ReplyDelete
  4. Hi i want to know what is the real purpose of this constructor chaining ? initializing varibles

    ReplyDelete
  5. As you know "In java every class is subclass of Object class." So to access Object class compiler uses constructor chaining. This is most use in Inheritance where we can access parent class method using child class object.

    ReplyDelete