What Is Cloning?
Cloning is a process of creating an exact copy of an existing object in the memory. In Java, clone() method of java.lang.Object class is used for cloning process. This method creates an exact copy of an object on which it is called through field-by-field assignment and returns the reference of that object. Not all the objects in java are eligible for cloning process. The objects which implement Cloneable interface are only eligible for cloning process. Cloneable interface is a marker interface which is used to provide the marker to cloning process. Click here to see more info on clone() method in Java.
Both shallow copy and deep copy are related to this cloning process. The default version of clone() method creates the shallow copy of an object. To create the deep copy of an object, you have to override the clone() method. Let’s see how these shallow copy and deep copy work.
Shallow Copy In Java :
The default version of clone() method creates the shallow copy of an object. The shallow copy of an object will have exact copy of all the fields of original object. If original object has any references to other objects as fields, then only references of those objects are copied into clone object, copy of those objects are not created. That means any changes made to those objects through clone object will be reflected in original object or vice-versa. Shallow copy is not 100% disjoint from original object. Shallow copy is not 100% independent of original object.
Below is the example which creates the shallow copy of an object ‘student1‘.
class Course
{
String subject1;
String subject2;
String subject3;
public Course(String sub1, String sub2, String sub3)
{
this.subject1 = sub1;
this.subject2 = sub2;
this.subject3 = sub3;
}
}
class Student implements Cloneable
{
int id;
String name;
Course course;
public Student(int id, String name, Course course)
{
this.id = id;
this.name = name;
this.course = course;
}
//Default version of clone() method. It creates shallow copy of an object.
protected Object clone() throws CloneNotSupportedException
{
return super.clone();
}
}
public class ShallowCopyInJava
{
public static void main(String[] args)
{
Course science = new Course("Physics", "Chemistry", "Biology");
Student student1 = new Student(111, "John", science);
Student student2 = null;
try
{
//Creating a clone of student1 and assigning it to student2
student2 = (Student) student1.clone();
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
//Printing the subject3 of 'student1'
System.out.println(student1.course.subject3); //Output : Biology
//Changing the subject3 of 'student2'
student2.course.subject3 = "Maths";
//This change will be reflected in original student 'student1'
System.out.println(student1.course.subject3); //Output : Maths
}
}
In the above example, ‘student1‘ is an object of ‘Student‘ class which has three fields – id, name and course. ‘course‘ is a reference variable pointing to a ‘Course‘ type object. Clone of ‘student1‘ is created by calling clone method on it and assigned it to ‘student2‘. As default version of clone method creates the shallow copy, the ‘course‘ field of both ‘student1‘ and ‘student2‘ will be pointing to same ‘Course‘ object. So, any changes made to this object through ‘student2‘ will be reflected in ‘student1‘ or vice-versa.
See the below picture for more clear understanding.
Deep Copy In Java :
Deep copy of an object will have exact copy of all the fields of original object just like shallow copy. But in additional, if original object has any references to other objects as fields, then copy of those objects are also created by calling clone() method on them. That means clone object and original object will be 100% disjoint. They will be 100% independent of each other. Any changes made to clone object will not be reflected in original object or vice-versa.
To create a deep copy of an object, you have to override the clone() method as demonstrated in the below example.
class Course implements Cloneable
{
String subject1;
String subject2;
String subject3;
public Course(String sub1, String sub2, String sub3)
{
this.subject1 = sub1;
this.subject2 = sub2;
this.subject3 = sub3;
}
protected Object clone() throws CloneNotSupportedException
{
return super.clone();
}
}
class Student implements Cloneable
{
int id;
String name;
Course course;
public Student(int id, String name, Course course)
{
this.id = id;
this.name = name;
this.course = course;
}
//Overriding clone() method to create a deep copy of an object.
protected Object clone() throws CloneNotSupportedException
{
Student student = (Student) super.clone();
student.course = (Course) course.clone();
return student;
}
}
public class DeepCopyInJava
{
public static void main(String[] args)
{
Course science = new Course("Physics", "Chemistry", "Biology");
Student student1 = new Student(111, "John", science);
Student student2 = null;
try
{
//Creating a clone of student1 and assigning it to student2
student2 = (Student) student1.clone();
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
//Printing the subject3 of 'student1'
System.out.println(student1.course.subject3); //Output : Biology
//Changing the subject3 of 'student2'
student2.course.subject3 = "Maths";
//This change will not be reflected in original student 'student1'
System.out.println(student1.course.subject3); //Output : Biology
}
}
Below picture shows how deep copy of ‘student1’ is created.
Shallow Copy Vs Deep Copy In Java :
Below is the list of differences between shallow copy and deep copy in Java.
Shallow Copy | Deep Copy |
Cloned Object and original object are not 100% disjoint. | Cloned Object and original object are 100% disjoint. |
Any changes made to cloned object will be reflected in original object or vice versa. | Any changes made to cloned object will not be reflected in original object or vice versa. |
Default version of clone method creates the shallow copy of an object. | To create the deep copy of an object, you have to override clone method. |
Shallow copy is preferred if an object has only primitive fields. | Deep copy is preferred if an object has references to other objects as fields. |
Shallow copy is fast and also less expensive. | Deep copy is slow and very expensive. |
Post a Comment