Cloning
Clone is a Greek word meaning “branch.” It is nothing but the process of copying one object to produce the exact same object.
What is the clone of an object in Java?
An object which is returned by the clone() method is known as a clone of an original instance. Clones are two separate objects in Java heap, having the same characteristics.
In Java, object cloning is of two types:
- Shallow Copy
- Deep Copy
Why Cloning?
clone() method is the fastest approach to copying an object. This process takes a fewer number of steps to accomplish.
How to Clone in java?
- By default, no Java class supports cloning but Java provides one interface called java.lang.Cloneable, a marker interface and by implementing this interface, we can make a duplicate copy of our object by calling clone() method of java.lang.Object class.
- clone() method is protected inside java.lang. Object class throws a java.lang.CloneNotSupportedException.
Protected Object clone() throws a CloneNotSupportedException.
Note
- By default, clone() method gives a shallow copy of the object; i.e., if we invoke super.clone(), then it’s a shallow copy.
- If we want to have a deep copy, we have to override the clone() method and make it public by giving our own definition of making a copy of the object.
Example
- public class Employee implements Cloneable
- {
- int id;
- String name;
-
- public String toString()
- {
- return "id:- " + id + " name:- " + name;
- }
- static public void main(String sd[]) throws CloneNotSupportedException
- {
- Employee e1 = new Employee();
- e1.id = 101;
- e1.name = "Shweta";
-
- Employee e2 = (Employee) e1.clone();
- System.out.println("Actual Object e1:- " + e1);
- System.out.println("Cloned Object e2:- " + e2);
- System.out.println("Actual object identity:- " + e1.hashCode());
- System.out.println("Cloned object identity:- " + e2.hashCode());
- }
-
- }
Output
Actual Object e1: id:- 101 name:- Shweta
Cloned Object e2: id:- 101 name:- Shweta
Actual object identity: 22068557
Cloned object identity: 29115481
Shallow Copy
Generally, clone() method of an object creates a new instance of the same class, and copies all the fields to the new instance and returns it. This is nothing but a shallow copy.
Object class provides a clone() method and provides support for the shallow copy. It returns ‘Object’ as a type and you need to explicitly cast back to your original object.
Note
If the original object has any references to other objects as fields, then only the references of those objects are copied into the clone object, a copy of those objects is not created.
Here, Employee e1 object has a reference of Address object a1, then Employee object e1 clone is created, but not of Address object a1.
Example
- Address.java
- public class Address
- {
- public String city, state, country;
- public Address(String city, String state, String country)
- {
- this.city = city;
- this.state = state;
- this.country = country;
- }
- }
- Employee.java
- public class Employee implements Cloneable
- {
- int id;
- String name;
- Address a1;
-
- public Employee(int id, String name, Address a1)
- {
- this.id = id;
- this.name = name;
- this.a1 = a1;
- }
- public String toString()
- {
- return "id:- " + id + " ,name:- " + name + " ,city:- " + a1.city + " ,state:- " + a1.state + " ,country:- " + a1.country;
- }
- static public void main(String sd[]) throws CloneNotSupportedException
- {
- Address a1 = new Address("Ghaziabad", "U.P.", "INDIA");
- Employee e1 = new Employee(101, "Shweta", a1);
-
- Employee e2 = (Employee) e1.clone();
-
- System.out.println("Actual Object:-\n" + e1);
- System.out.println("Cloned Object:- \n" + e2);
-
-
- System.out.println("Actual Object with address object:-\n" + e1.a1.hashCode());
- System.out.println("Cloned Object with address object:- \n" + e2.a1.hashCode());
- }
-
- }
Output
Actual Object: id:- 101 ,name:- Shweta ,city:- Ghaziabad ,state:- U.P. ,country:- INDIA
Cloned Object: id:- 101 ,name:- Shweta ,city:- Ghaziabad ,state:- U.P. ,country:- INDIA
Actual Object with address object: 8152936
Cloned Object with address object: 8152936
Deep Copy
A deep copy of an object will have an exact copy of all the fields of the original object, just like the shallow copy. If the original object has any references to the other objects as fields, then a copy of those objects is also created by calling clone() method on them.
Note: To create a deep copy of an object, you must override the clone().
Example
- Address.java
- public class Address implements Cloneable
- {
- public String city, state, country;
- public Address(String city, String state, String country)
- {
- this.city = city;
- this.state = state;
- this.country = country;
- }
- protected Object clone() throws CloneNotSupportedException
- {
- return super.clone();
- }
- }
- Employee.java
- public class Employee implements Cloneable
- {
- int id;
- String name;
- Address a1;
-
- public Employee(int id, String name, Address a1)
- {
- this.id = id;
- this.name = name;
- this.a1 = a1;
- }
- public String toString()
- {
- return "id:- " + id + " ,name:- " + name + " ,city:- " + a1.city + " ,state:- " + a1.state + " ,country:- " + a1.country;
- }
-
- public Object clone() throws CloneNotSupportedException
- {
- Employee e1 = (Employee) super.clone();
-
- e1.a1 = (Address) a1.clone();
-
- return e1;
- }
- static public void main(String sd[]) throws CloneNotSupportedException
- {
- Address a1 = new Address("Ghaziabad", "U.P.", "INDIA");
- Employee e1 = new Employee(101, "Shweta", a1);
-
- Employee e2 = (Employee) e1.clone();
-
- System.out.println("Actual Object:-\n" + e1);
- System.out.println("Cloned Object:- \n" + e2);
-
-
- System.out.println("Actual Object with address object:-\n" + e1.a1.hashCode());
- System.out.println("Cloned Object with address object:- \n" + e2.a1.hashCode());
- }
-
- }
Output
Actual Object: id:- 101 ,name:- Shweta ,city:- Ghaziabad ,state:- U.P. ,country:- INDIA
Cloned Object: id:- 101 ,name:- Shweta ,city:- Ghaziabad ,state:- U.P. ,country:- INDIA
Actual Object with address object: 23660326
Cloned Object with address object: 5538765
Shallow Copy vs. Deep Copy in Java
Shallow Copy |
Deep Copy |
Cloned object and original object are not 100% different. |
Cloned object and original object are 100% different |
Shallow copy is preferred if an object has only primitive fields. |
Deep copy is preferred if an object has references to other objects as the fields. |
Shallow copy is fast |
Deep copy is slow |
No need to override clone(). |
Must have to override clone() method. |