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
xxxxxxxxxx
public class Employee extends Person
{
private String name;
public Employee(String name)
{
this.name = name;
}
public Employee(Employee emp)
{
this.name = emp.name;
}
}
xxxxxxxxxx
// copy constructor
public class Fruits
{
private double price;
private String name;
//copy constructor
public Fruits(Fruits fruits)
{
//copying each filed
this.price = fruits.price; //getter
this.name = fruits.name; //getter
}
}