Java Object Cloning (Shallow & Deep Copy)
Introduction
In Java, object cloning refers to the process of creating an exact copy of an existing object. Java provides built-in mechanisms to perform cloning using the Cloneable
interface and the clone()
method of the Object
class. Cloning is mainly of two types:
-
Shallow Copy - Creates a copy of the object but not the objects referenced by it.
-
Deep Copy - Creates a copy of the object along with all referenced objects.
Understanding the Cloneable Interface
The Cloneable
interface is a marker interface that must be implemented by a class whose objects are intended to be cloned. If a class does not implement Cloneable
, calling the clone()
method on its object will throw CloneNotSupportedException
.
Implementing Shallow Copy
A shallow copy creates a new object but does not copy the referenced objects within it. Instead, it copies the reference, meaning both objects share the same referenced objects.
Example of Shallow Copy:
class Address {
String city;
Address(String city) {
this.city = city;
}
}
class Employee implements Cloneable {
String name;
Address address;
Employee(String name, Address address) {
this.name = name;
this.address = address;
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class ShallowCopyExample {
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address("New York");
Employee emp1 = new Employee("John", address);
Employee emp2 = (Employee) emp1.clone();
System.out.println("Before Modification:");
System.out.println("Emp1 Address: " + emp1.address.city);
System.out.println("Emp2 Address: " + emp2.address.city);
emp2.address.city = "Los Angeles"; // Modifying emp2 also modifies emp1
System.out.println("After Modification:");
System.out.println("Emp1 Address: " + emp1.address.city);
System.out.println("Emp2 Address: " + emp2.address.city);
}
}
Output:
Before Modification:
Emp1 Address: New York
Emp2 Address: New York
After Modification:
Emp1 Address: Los Angeles
Emp2 Address: Los Angeles
This demonstrates that changing the address.city
in emp2
also changes it in emp1
, proving that only the reference was copied.
Implementing Deep Copy
A deep copy creates a new object along with new instances of all the referenced objects.
Example of Deep Copy:
class Address implements Cloneable {
String city;
Address(String city) {
this.city = city;
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Employee implements Cloneable {
String name;
Address address;
Employee(String name, Address address) {
this.name = name;
this.address = address;
}
public Object clone() throws CloneNotSupportedException {
Employee cloned = (Employee) super.clone();
cloned.address = (Address) address.clone(); // Creating a new Address object
return cloned;
}
}
public class DeepCopyExample {
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address("New York");
Employee emp1 = new Employee("John", address);
Employee emp2 = (Employee) emp1.clone();
System.out.println("Before Modification:");
System.out.println("Emp1 Address: " + emp1.address.city);
System.out.println("Emp2 Address: " + emp2.address.city);
emp2.address.city = "Los Angeles"; // Modifying emp2 does NOT modify emp1
System.out.println("After Modification:");
System.out.println("Emp1 Address: " + emp1.address.city);
System.out.println("Emp2 Address: " + emp2.address.city);
}
}
Output:
Before Modification:
Emp1 Address: New York
Emp2 Address: New York
After Modification:
Emp1 Address: New York
Emp2 Address: Los Angeles
Since we performed a deep copy, modifying emp2.address.city
does not affect emp1.address.city
.
Differences Between Shallow Copy and Deep Copy
Aspect | Shallow Copy | Deep Copy |
---|---|---|
Object Duplication | Only the main object is copied. | The main object and referenced objects are copied. |
Reference Sharing | Copied object shares references of the original. | New references are created for cloned objects. |
Changes in Cloned Object | Affects the original object. | Does not affect the original object. |
Implementation | Uses super.clone() directly. |
Needs explicit cloning of referenced objects. |
When to Use Which?
-
Shallow Copy: When an object contains immutable fields or when changes to referenced objects should reflect across copies.
-
Deep Copy: When complete isolation between copies is needed.
Conclusion
Java provides object cloning via the Cloneable
interface and the clone()
method. Understanding shallow and deep copies helps in making informed decisions when dealing with object duplication. While shallow copies are quick and memory efficient, deep copies ensure data integrity by maintaining separate object references.
Sign up here with your email
ConversionConversion EmoticonEmoticon