what is constructor chaining ?????

explain with example in detail how to solve it program plz send a program

Your Answer

Constructor chaining is the process of calling one constructor from another constructor with respect to current object.
// 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();
    }
}
Run on IDE
Output:
The Default constructor 5 75

Core Java

Didn't get the answer.
Contact people of Talent-Core Java directly by clicking here