We can call constructor from another class. It is called constructor chaining.
To achieve constructor chaining we use this reference.
This must be the first statement inside constructor.
Using constructor chaining, we reduce the efforts of developer.
We can not use this() and super() inside same constructor.
Because this() refers to current class constructor and super() refers to parent class constructor
Example:
Class Employee{
public int empId;
public String empName;
public String companyName;
Public Employee()
{
this(123,”bob”,”vodafone”);
}
public Employee(int empId,String empName,String companyName)
{
this.empId=empId;
this.empName=empName;
this.companyName=companyName
}
}
Comments
Post a Comment