what is constructor chaining ?????

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


8 Answers
1-8 of  8
8 Answers
  • Constructor chaining is the process of calling one constructor from another constructor with respect to current object. Constructor chaining can be done in two ways: Within same class: It can be done using this() keyword for constructors in same class.

  • 1.Constructor chaining is the process of calling one constructor from another constructor with respect to current object.
    2.Constructor chaining can be done in two ways:
     a).Within same class: It can be done using this() keyword for constructors in same class
    b).From base class: by using super() keyword to call constructor from the base class.
    -->Constructor chaining occurs through inheritance.
    A sub class constructor’s task is to call super class’s constructor first. This ensures that creation of sub class’s object starts with the initialization of the data members of the super class. There could be any numbers of classes in inheritance chain. Every constructor calls up the chain till class at the top is reached.
    // 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();
        }
    }
    Output:
    The Default constructor
    5
    75

  • One constructor calling another constructor from another class It can be performed by using super() method

  • calling superclass constructor while creating sub class object is known as constructor chain.
    For example:
    class Person   //super class
    {
     String name;
     Person(String n)
     {
       name=n;
     } 
    }
    class Teacher extends Person    //subclass
    {
     String sub;
     Teacher(String n,String s)
     {
      super(n);
      sub=s;
     }
    }
    class Test
    {
     public static void main(String[] args)
    {
     Teacher t=new Teacher("schildt","java");
     System.out.println(t.name);
    System.out.println(t.sub);
    }
    }

  • If sub class C calling its super class B constructor, and the that super class B constructor calling its super class A constructor then such type of constructor call known as constructor chaining.
    example:
    ​if class A ------->super class
    ​B  ------->super /sub class
    ​C ------>sub class

  • 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

  • Constructor chaining is the process of calling one constructor from another constructor with respect to current object
    it can be done two ways  i.e.,1.within the class 
                                                    2.from base class

Core Java

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