Shallow Copy:
A new object will be created that has an exact copy values of its original object. Means, it will copy all of the fields , suppose if any of the fields are object reference then reference address only will be copied. That is, reference address means, memory address.
![]() |
| Before shallow copy picture 1 |
![]() |
| After Shallow copy picture 2 |
We have added here two pics, and first picture mentioning before shallow copy and second picture is after doing shallow copy. Now lets see points,
1. Initially Main object 1 have filed 1 and contain object 1.
2. After doing shallow copy Main Object 2 is created with filed 2.
3. But still contain object 2 is not created and Main object 2 is pointing contain object 1. Means, memory address copied. (reference address).
4. So if you will do any changes in Contain object 1 of Main object 1, it will reflect in Main object 2 also.
Deep copy
It will copy all of the fields and all the dynamically allocated memory address pointed by that object are also copied. Below picture will say clearly.![]() | |||||
| Before Deep Copy |
![]() |
| After Deep copy |
1. Initially Main object 1 have filed 1 and contain object 1.
2. After doing deep copy Main Object 2 is created with filed 2 and contain object 2.
3. Remember here, contain object 2 was not created in shallow, but in deep contain object 2 is created.
4. So if you will do any changes in Contain object 1 of Main object 1, it will not reflect in Main object 2 .
Shallow copy with example code
public class Subject {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Subject(String name) {
this.name = name;
}
}
Create another one class called Student and code is belowpublic class Student implements Cloneable {
// Contained object
private Subject subj;
private String name;
public Subject getSubj() {
return subj;
}
public void setSubj(Subject subj) {
this.subj = subj;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(String name, String sub) {
this.name = name;
this.subj = new Subject(sub);
}
public Object clone() {
// Create shallow copy
try {
return super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}
Below class is to test shallow copypublic class ShallowTest {
public static void main(String[] args) {
// Original Object
Student stud = new Student("RAJA", "MATHS");
System.out.println("Original Object : " + stud.getName() + " ---- "
+ stud.getSubj().getName());
// Create Clone Object
Student clonedStud = (Student) stud.clone();
System.out.println("After Cloned Object: " + clonedStud.getName() + " ---- "
+ clonedStud.getSubj().getName());
//Again setting new data
stud.setName("Kumar");
stud.getSubj().setName("CHEMISTRY");
System.out.println("Original Object after updating: "
+ stud.getName() + " ---- " + stud.getSubj().getName());
System.out.println("Cloned Object after updating original object: "
+ clonedStud.getName() + " ---- "
+ clonedStud.getSubj().getName());
}
}
OutputOriginal Object : RAJA ---- MATHS
After Cloned Object: RAJA ---- MATHS
Original Object after updating : Kumar ---- CHEMISTRY
Cloned Object after updating original object: RAJA - CHEMISTRY
Deep copy with example code
You can use the above shallow example and do a Little bit change like below and execute it.
public class Student implements Cloneable {
// Contained object
private Subject subj;
private String name;
public Subject getSubj() {
return subj;
}
public void setSubj(Subject subj) {
this.subj = subj;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(String name, String sub) {
this.name = name;
this.subj = new Subject(sub);
}
public Object clone() {
// For deep copy
Student student = new Student(name, subj.getName());
return student;
}
}
The only difference here, we are creating new object inside clone method and returning. 



No comments:
Post a Comment