Showing posts with label Shallow copy. Show all posts
Showing posts with label Shallow copy. Show all posts

Shallow copy in Java


Shallow copy in Java


In one of our earlier tutorial we have seen about Java clone. In this tutorial we will see about Shallow Copy with simple example code. Also we will about What is Shallow Copy and how its works. Before we knowing about Shallow Copy or Shallow Clone we need to know about what is clone and how its working in Java upon Objects.

As already we have seen in our previous tutorial Clone is nothing but the process of copying one Object to produce the exact copy, but not guaranteed for all Objects can be cloned. Suppose if the Object is not supported for cloning then we can't make another copy, in that case we will get CloneNotSupportedException Exception. If the class is Fully singleton class and as per rule we can't make another copy of the class, hence it will be a best example for clone not allowed. Cloning can be divided into two types as 

  • Shallow Copy 
  • Deep Copy

In this tutorial we will see about only Shallow copy with simple example code and how its working. Shallow copy is nothing but default clone implementation where it create new instance and copy all the field of object to the new instance and returns the Object type.  Object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, it holds just the address reference of the other Objects rather copying complete instance is called Shallow copy. 

Explicitly we need to type cast to our class as given in below example. Basically if a class need to support cloning then we need to implement Cloneable interface and need to Override clone method also. 

Now all we know that clone will be Shallow or Deep copy to create a new copy of the class object. Here interviewer will interrupt and put a question like when we clone object whether Class constructor will be called or not? Suddenly our ears will open widely and start thinking. Answer will be NO, while cloning any class object constructor will not be called. 

Lets see simple example for Shallow copy and how its works.


public class Subject{
 
 String name;
 
 public Subject(){
  System.out.println("Inside Default Constructor");
 }
 
 public Subject(String name){
  System.out.println("Inside 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 super.clone();
 }
}



public class ShallowCopy {

 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 shallow Object contains other Objects which will have only
   * the address reference. Hence by below line Language will change 
   * in both the Objects (Original as well as in Cloned)
   */
  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

Clone Student Name: Raj
Clone Student Sub : Hindi

Original Student Name: Raj
Original Student Sub : Tamil

Clone Student Name: David
Clone Student Sub : Tamil



In above output we can see Subject ("Tamil") has changed for both the Objects, original as well as Cloned Object as we have changed Subject Name only for cloned Object in our code. 
Its the example for Shallow copy where Object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, it holds just the address reference of the other Objects rather copying complete instance into cloned instance.