Deep Copy in Java

 


Deep Copy in Java
In our earlier tutorial we have seen about Shallow copy and how we can implement and how its works in Java. Same way we will see about Deep copy in this tutorial.

Deep copy - by name itself we can identify as its copy complete Object in-depth while cloning any instance. In Shallow copy we have seen that Object which holds another Object will not be copied and only holds the address reference. Where as in Deep copy it copies those Objects also and creates the instance for those Objects. Apart from this difference Deep copy is same as Shallow copy like, explicitly we need to type cast to our class and Constructors will not be called in our cloning class, but Object which contains other Object class constructors will be called while cloning from clone() method instance creation. 


We will see same example which we have seen in Shallow copy and make changes only in our clone() method to implement Deep Copy.


public class Subject{
 
 String name;
 
 public Subject(){
  System.out.println("Inside Default Constructor");
 }
 
 public Subject(String name){
  System.out.println("\nInside Constructor");
  this.name = name;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 } 
}



public class Student implements Cloneable{

 String name;
 Subject sub;
 
 public Student(String name, String sub){
  this.name = name;
  this.sub = new Subject(sub);
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Subject getSub() {
  return sub;
 }
 public void setSub(Subject sub) {
  this.sub = sub;
 } 
 
 @Override
 protected Object clone() throws CloneNotSupportedException {
  return new Student(name, sub.getName());
 }
}



public class DeepCopy {

 public static void main(String[] args) throws CloneNotSupportedException {
  
  Student stu = new Student("Raj", "Hindi");
  
  System.out.println("\nOriginal Student Name: "+stu.getName());
  System.out.println("Original Student Sub : "+stu.getSub().getName());
  
  // Cloning the original Object and Explicitly type casting 
  Student stu1 = (Student)stu.clone();
  
  System.out.println("\nClone Student Name: "+stu1.getName());
  System.out.println("Clone Student Sub : "+stu1.getSub().getName());
  
  stu1.setName("David");
  
  
  /*
   * In Deep copy cloned Object will hold complete copy of original Object
   */
  stu1.getSub().setName("Tamil");
  
  System.out.println("\nOriginal Student Name: "+stu.getName());
  System.out.println("Original Student Sub : "+stu.getSub().getName());
  
  System.out.println("\nClone Student Name: "+stu1.getName());
  System.out.println("Clone Student Sub : "+stu1.getSub().getName());
 }
}


OUTPUT:


Inside Constructor

Original Student Name: Raj
Original Student Sub : Hindi

Inside Constructor

Clone Student Name: Raj
Clone Student Sub : Hindi

Original Student Name: Raj
Original Student Sub : Hindi

Clone Student Name: David
Clone Student Sub : Tamil



In above output we can see Subject name ("Tamil") has changed only for the cloned Object, where as in Shallow copy it holds the address of Object. 
    




No comments:
Write comments