A)
->from within one constructor of a class calling another constructor of the same class(object) is known as constructor chaining.
-> ”this” keyword is used to implement constructor chaining.
//constructor chaining
class employees
{
int empno;
employees()
{
System.out.println("one employee appointed");
}
employees(int eno)
{
this(); //constructor chaining
empno=eno;
}
void display()
{
System.out.println("epno :"+empno);
}
}
class constructorchaining {
public static void main(String[] args) {
employees e = new employees(1001);
e.display();
}
}
Output :
one employee appointed
epno :1001
->the purpose of constructor chaining is at the time of object creation only one constructor is executed. But at that time we want to call 2 or more constructors this constructor chaining is used with the help of “this” keyword.
Comments
Post a Comment