Deep Copy Object Cloning in Java


An alternative is a deep copy, meaning that fields are dereferences: rather than references to objects being copied, new copy objects are created for any referenced objects, and references to these placed in B. The result is different from the result a shallow copy gives in that the objects referenced by the copy B are distinct from those referenced by A, and independent.

Deep copies are more expensive, due to needing to create additional objects, and can be substantially more complicated, due to references possibly forming a complicated graph.

The program demonstrates deep cloning of an object in Java using the clone() method.

  • The program defines two classes Employee and Designation. The Employee class has three fields name, age, and deg of types String, int, and Designation respectively. The Designation class has one field role of type String.
  • The Employee class implements the Cloneable interface and overrides the clone() method to create a deep copy of the object. The clone() method creates a shallow copy of the object using super.clone() method and then creates a new object of Designation and assigns it to the deg field of the shallow copy, making it a deep copy.
  • In the main() method, an object e1 of the Employee class is created and its fields are set. Then e1 is displayed.
  • Next, a clone of e1 is created using the clone() method and stored in e2. The role field of the deg object of e2 is changed to "HR" and then e2 is displayed.
  • Finally, the e1 object is displayed again to show that its deg field has not been changed due to deep cloning.

Source Code

//Deep Copy Object Cloning in Java
class Designation
{
    String role;
    @Override
    public String toString() {
        return role;
    }
}
class Employee implements Cloneable
{
    String name;
    int age;
    Designation deg=new Designation();
 
    public void display()
    {
        System.out.println("Name       : "+name);
        System.out.println("Age        : "+age);
        System.out.println("Department : "+deg);
        System.out.println("----------------------");
    }
    public Object clone() throws CloneNotSupportedException
    {
 
        // Assign the shallow copy to new reference variable e
        Employee e = (Employee)super.clone();
 
        // Creating a deep copy for deg (Designation)
        e.deg = new Designation();
        e.deg.role = deg.role;
 
        // Create a new object for the field e
        // and assign it to shallow copy obtained,
        // to make it a deep copy
        return e;
    }
 
 
}
public class objectCloningDeepDemo {
    public static void main(String[] args) throws CloneNotSupportedException {
 
        Employee e1 =new Employee();
        e1.name="Ram Kumar";
        e1.age=25;
        e1.deg.role="Manager";
        System.out.println("Before Changing Role : ");
        e1.display();
 
        System.out.println("Cloning and Printing E2 object : ");
        Employee e2 = (Employee)e1.clone();
        e2.deg.role="HR";
        e2.display();
 
        System.out.println("After Changing Role : ");
        e1.display();
 
    }
}
 

Output

Before Changing Role :
Name       : Ram Kumar
Age        : 25
Department : Manager
----------------------
Cloning and Printing E2 object :
Name       : Ram Kumar
Age        : 25
Department : HR
----------------------
After Changing Role :
Name       : Ram Kumar
Age        : 25
Department : Manager
----------------------
To download raw file Click Here

Basic Programs