The Process of creating duplicate objects (exactly the same) is known as cloning. The main purpose of cloning is to maintain a backup copy and preserve the state of an object.
In Java, we can perform the cloning by using the clone method present in the Object class.
protected Object clone() throws CloneNotSupportedException {
}
We can perform cloning only for Cloneable objects. An object is clonable if the corresponding class implements a Cloneable interface. The Cloneable interface is present in Java. lang package and there is no method present in this interface. It is a marker interface.
Shallow vs Deep Cloning
Java default clone() method performed a shallow copy.
Shallow Cloning
- Shallow cloning is a process where a new object is created and all the field of thr orignal object are copied to the new object.
- If the original object contains a primitive variable then the exact duplicate copy will be created in the clone object or new object.
- If the original object contains any reference variable(object, then the reference of these objects is copied not the object themselves.
class Address{
String city;
public Address(String city) {
this.city = city;
}
}
class PersonDetails implements Cloneable{
String name;
Address address;
public PersonDetails(String name, Address address) {
this.name = name;
this.address = address;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class ShallowCloning {
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address("Delhi");
PersonDetails per = new PersonDetails("Java",address);
PersonDetails perClone = (PersonDetails) per.clone();
perClone.address.city = "NewDelhi"; // changing the city in cloned object
System.out.println(per.address.city); // orignal object field modified
}
}
- In shallow cloning by using clone object reference, if we perform any change in the clone object then these changes will be reflected in the original object. To overcome this problem we should go for deep cloning.
Deep Cloning
- The process of creating exactly duplicate independent copies including contained objects is known as deep cloning.
- In deep cloning, if the original object contains any primitive variable then in the clone object duplicate copies will be created.
- If the original object creat any reference variable then the corresponding object will also created in the cloned object.
- By default Object class clone() method performs shallow cloning but we can implement deep cloning by overriding the clone method in our class.
class Address{
String city;
public Address(String city) {
this.city = city;
}
}
class PersonDetails implements Cloneable{
String name;
Address address;
public PersonDetails(String name, Address address) {
this.name = name;
this.address = address;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Address add = new Address(address.city);
PersonDetails clonedPerson = new PersonDetails(name,add);
return clonedPerson;
}
}
public class ShallowCloning {
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address("Delhi");
PersonDetails per = new PersonDetails("Java",address);
PersonDetails perClone = (PersonDetails) per.clone();
perClone.address.city = "NewDelhi"; // changing the city in cloned object
System.out.println(per.address.city); // orignal object field not changed
}
}
- In Deep Cloning by using cloned object reference. If we perform any change to the contained object those changes will not be reflected in the original object.
Which Cloning is best Deep Cloning or Shallow Cloning?
- If the object contains only primitive variable Shallow Cloning is the best choice.
- If the object contains any reference variable then Deep Cloning is the best choice.
6 thoughts on “clone() Method in Java”