clone with shallow and deep copy

clone() with shallow and deep copy


    clone()

  • Clone( ) is used to create duplicate object to current object with same state of the current Object .

  • Clone( ) method is one of the important methods available under Object class.

  • Clone( ) will give deap copy of duplicate Objects, incase of current Object it contains only primitive values.

  • Clone( ) will give Shallow copy of duplicate Objects, incase of current Object it contains any derived type of attribute.

  • Deap Copy:

  • Deap copy is 100% disjoint between the current object and duplicate object .

  • If we change the state of object it won’t effect to the duplicate object.

  • Shallow Copy:

  • Shallow copy is not disjoint means if we change the state of the duplicate object it will effect to the original object.

  • If we change the state of object it won’t effect to the duplicate object.

  • Syntax of clone method :


    protected Object clone( ) throws CloneNotSupportedException

  • hashCode ( ) will always try to prove two objects to be equal.

  • If we want to use clone( ) we should take care of one checkedtype Exception - i.e. CloneNotSupportedException.

  • By using clone( ) we are performing unsecured operation, by default JVM will not allow to perform any unsecured operations.

  • In order to make this unsecured operation, then we should implements the clonable interface.

  • Cloneable interface is a marker interface, it won’t give any features to our class it is mainly used for the JVM purpose only in order to blame the JVM for performing unsecured operation.

  • Marker interface is an empty interface and not every empty interface is a marker interface.

  • Marker interface is only for JVM purpose not for the developement purpose.


  • In order to perform clone operation that class must implements cloneable interface. This is a marker interface for blaming JVM in order to perform unsecured operation.

    Example 1:

     
                    
                 package com.sdj;
                 import java.io.IOException;
                 class Person implements Cloneable
                 {
                    int age;
                    /* clone( ) access level is protected in object class incase of */
                    /* inheritance it is specific to the subclass and */
                    /* we can’t access outside of the class. */
                   public Person getDuplicate()throws CloneNotSupportedException
                   {
                      Person dup=(Person) this.clone();
                      /*here we are doing downcasting from object type to Person type*/
                      return dup;
                   }
                 }
                 public class CloneDemo 
                 {
                    public static void main(String[] args) throws CloneNotSupportedException 
                    {
                       Person p=new Person();
                       p.age=25;
                       Person p1=p.getDuplicate();
                       System.out.println(p1.age);
                       p1.age=34;
                       System.out.println(p.age);
                       System.out.println(p1.age);
                    }
                 }                

       Output:

    25 25 34

    Explanation:

    In the above program getDuplicate( ) is defined in the Person class.Inside this method we are downcasting object type into person type means clone( ) will return object type.

    • In the above program p is a original object and p1 is a duplicate object. Here p object contains only primitive value means the duplicated object is Deap Copy

    • Even though we modify the p1(duplicate) object value it won’t effect to the p(original) object.

    Shallow Copy

    • Shallow copy is not 100% disjoint. If there are derived attributes inside the original objects then only shallow copy gets created .

    Example 2:

     
                    
                 package com.sdj;
                 class Address implements Cloneable
                 {
                    int StreetNo;
                    int pin;
                 }
                 class Person implements Cloneable
                 {
                    int age;
                    double height;
                    Address adr;
                    Person(int age,double height,Address adr ) 
                    {
                       this.age=age;
                       this.height=height;
                       this.adr=adr;
                    }
                    /* clone( ) access level is protected in object class incase of inheritance */
                    /* it is specific to the subclass and we can’t access out side of the class.*/
                    public Person getClone() throws CloneNotSupportedException
                    {
                       return  (Person)this.clone();
                    }
                 }
                 public class ShallowCopy 
                 {
                    public static void main(String[] args)throws CloneNotSupportedException 
                    {
                       Address adr =new Address();
                       adr.pin=517126;
                       adr.StreetNo=3;
                       Person p1=new Person(24,5.8,adr);
                       Person p2=p1.getClone();
                       System.out.println(p2.age);
                       System.out.println(p1.age);
                       p2.adr.StreetNo=7;
                       System.out.println(p1.adr.StreetNo);
                       p2.adr.pin=517123;
                       System.out.println(p1.adr.pin);
                       p1.adr.pin=517666;
                       System.out.println(p2.adr.pin);
                    }
                 }                

       Output:

    24 24 7 517123 517666

    Explanation:

    In the above program getDuplicate( ) is defined in the Person class.Inside this method we are downcasting object type into person type means clone( ) will return object type.

    • In the above program Address is derived type attribute inside the Person class.

    • If cloneable object contains any derived type attribute then that class also must implements cloneable interface i.e. Address implements Cloneable.

    • So derived type attribute class and cloneable type class must implements cloneable interface I.e. Address implements Cloneable and Person implements cloneable

    • If we change the state of the p1 object it will automatically effect to the p2 object. I,e. p1 StreetNo, pin are changed by the p2 object.

    • However clone( ) with shallow copy is not advisable.

    • According to Object class we can get deep copy in case of all primitive types and we can get ShallowCopy in case of Object contains any derived type

    • If we want to get the Deep copy eventhough object contains any derived type then we should override clone( ) inside our class.

    • javapadho
      Memory manage for CloneNotSupportException

    Overriding clone( ):

      In order to override clone( ) our class must implements cloneable interface if our class contains any derived type then that class also must implements cloneable interface.

      Example 3:

       
                      
                   package com.sdj;
                   class Address implements Cloneable
                   {
                      int StreetNo;
                      int pin;   
                   }
                   class Person implements Cloneable
                   {
                      int age;   double height;
                      Address adr;
                      Person(int age,double height,Address adr ) 
                      {
                         this.age=age;
                         this.height=height;
                         this.adr=adr;   
                      }
                         /*overriding clone( )*/ 
                      public Object clone() throws CloneNotSupportedException 
                      {
                         Address add=new Address();
                         add.StreetNo=this.adr.StreetNo;
                         add.pin=this.adr.pin;
                         Person p3=new Person(this.age,this.height,add);
                         return p3;
                      }
                   }
                   public class ShallowCopy 
                   {
                      public static void main(String[] args)throws CloneNotSupportedException 
                      {
                         Address adr =new Address();
                         adr.pin=517126;   adr.StreetNo=3;
                         Person p1=new Person(25,5.8,adr);
                         Person p2=(Person)p1.clone();
                         System.out.println(p2.adr.pin);
                         p2.adr.pin=517123;
                         System.out.println(p1.adr.pin);
                         System.out.println(p2.adr.pin);
                         p2.adr.StreetNo=7;
                         System.out.println(p1.adr.StreetNo);
                         System.out.println(p2.adr.StreetNo);
                         p1.adr.StreetNo=6;
                         System.out.println(p1.adr.StreetNo);
                         P1.adr.pin=517333;
                         System.out.println(p1.adr.pin);
                      }
                   }                

         Output:

      517126 517126 517123 3 7 6 517333

      Explanation:

      • After overriding clone( ) p1 object changes will not effect to the p2 object because two objects have two derived objects created. one belongs to p1 object and another one belongs to p2 object.

      • In the above program p1 object pin,streetNo are not modified by the p2 object.

      • javapadho
        Memory manage when we use Overriding cloe()