explain with example in detail how to solve it program plz send a program
1.Constructor chaining is the process of calling one constructor from another constructor with respect to current object.br /2.Constructor chaining can be done in two ways:br /strong a).Within same class/strong: It can be done using strongthis()/strong keyword for constructors in same classbr /strongb).From base class: /strong by using strongsuper()/strong keyword to call constructor from the base class.br /-->Constructor chaining occurs through stronginheritance/strong.br /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.br /// Java program to illustrate Constructor Chainingbr /// within same class Using this() keywordbr /class Tempbr /{br / // default constructor 1br / // default constructor will call another constructorbr / // using this keyword from same classbr / Temp()br / {br / // calls constructor 2br / this(5);br / System.out.println("The Default constructor");br / }br / br / // parameterized constructor 2br / Temp(int x)br / {br / // calls constructor 3br / this(5, 15);br / System.out.println(x);br / }br / br / // parameterized constructor 3br / Temp(int x, int y)br / {br / System.out.println(x * y);br / }br / br / public static void main(String args[])br / {br / // invokes default constructor firstbr / new Temp();br / }br /}br /Output:br /The Default constructorbr /5br /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.br /For example:br /class Person //super classbr /{br / String name;br / Person(String n)br / {br / name=n;br / } br /}br /class Teacher extends Person //subclassbr /{br / String sub;br / Teacher(String n,String s)br / {br / super(n);br / sub=s;br / }br /}br /class Testbr /{br / public static void main(String[] args)br /{br / Teacher t=new Teacher("schildt","java");br / System.out.println(t.name);br /System.out.println(t.sub);br /}br /}
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.br /example:br /if class A ------->super classbr /B ------->super /sub classbr /C ------>sub class
Constructor chaining is the process of calling one constructor from another constructor with respect to current object.br /// Java program to illustrate Constructor Chainingbr /// within same class Using this() keywordbr /class Tempbr /{br / // default constructor 1br / // default constructor will call another constructorbr / // using this keyword from same classbr / Temp()br / {br / // calls constructor 2br / this(5);br / System.out.println("The Default constructor");br / }br / br / // parameterized constructor 2br / Temp(int x)br / {br / // calls constructor 3br / this(5, 15);br / System.out.println(x);br / }br / br / // parameterized constructor 3br / Temp(int x, int y)br / {br / System.out.println(x * y);br / }br / br / public static void main(String args[])br / {br / // invokes default constructor firstbr / new Temp();br / }br /}br /Run on IDEbr /Output:br /The Default constructor 5 75
Constructor chaining is the process of calling one constructor from another constructor with respect to current objectbr /it can be done two ways i.e.,1.within the class br / 2.from base class
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.