What is Copy-Constructor?

Solution:

Finally, Java does support copy constructors like C++, but the difference lies in the fact that Java does not create a default copy constructor if you do not write your own.

Copy constructor for Employee class is shown below:

Copy Constructor

public class Employee extends Person   
{  
    private String name;
   
    public Employee(String name)
    {
      this.name = name;
    }
   
    public Employee(Employee emp)
    {
      this.name = emp.name;
    }
   
}
0 answers