Google News
logo
Java Constructor - Interview Questions
What is Constructor Chaining ?
Constructor Chaining is a technique of calling another constructor from one constructor. this() is used to call same class constructor where as super() is used to call super class constructor.
 
// Java program to illustrate Constructor Chaining
// within same class Using this() keyword
class Temp
{
    // default constructor 1
    // default constructor will call another constructor
    // using this keyword from same class
    Temp()
    {
        // calls constructor 2
        this(5);
        System.out.println("The Default constructor");
    }
  
    // parameterized constructor 2
    Temp(int x)
    {
        // calls constructor 3
        this(5, 15);
        System.out.println(x);
    }
  
    // parameterized constructor 3
    Temp(int x, int y)
    {
        System.out.println(x * y);
    }
  
    public static void main(String args[])
    {
        // invokes default constructor first
        new Temp();
    }
}

 

Advertisement