Cloning

What is the clone of an object in Java?
- Shallow Copy
- Deep Copy
Why Cloning?
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()); //both e1 and e2 object are separate objects inside heap
- }
- }
Cloned Object e2: id:- 101 name:- Shweta
Actual object identity: 22068557
Cloned object identity: 29115481

- 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);
- //Proof of same a1 object with both e1 and 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()); //both have same 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
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

- 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);
- //Proof of different Address object with e1 and 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()); //both have different 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
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 | 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. |
Vignesh ManiPosted Jun 17, 2016, 5:20 AM
Good one
Debasis SahaPosted Jun 17, 2016, 12:50 AM
Nice one..
RajaPosted Jun 17, 2016, 12:46 AM
Nice Explanation...
Thiruppathi RPosted Jun 16, 2016, 11:04 PM
Nice...