Shallow Cloning

1). The process of creating a bitwise copy of an object is called Shallow Cloning.

2). If the main object contains any primitive variables then exactly duplicate copies will be created in cloned objects.

3). If the main object contains any reference variable then the corresponding object won't be created just a reference variable will be created by pointing to the old contained object.

4). By using main object reference if we perform any change to the contained object then those changes will be reflected automatically to the cloned object, by default Object class clone( ) meant for Shallow Cloning.

class Cat {
int j ; Cat(int j) { this.j=j ; } } class Dog implements Cloneable { Cat c ; int i ; Dog(Cat c , int i) { this.c=c ; this.i=i ; } public Object clone( ) throws CloneNotSupportedException { return super.clone( ); } }
public class ShallowClone {
public static void main(String[ ] ar) throws CloneNotSupportedException { Cat c=new Cat(20) ; Dog d1=new Dog(c , 10) ; System.out.println(d1.i +"......"+d1.c.j); // 10......20 Dog d2=(Dog)d1.clone( ) ; d1.i=888 ; d1.c.j=999 ; System.out.println(d2.i +"......"+d2.c.j); // 10......999 } }


6). Shallow cloning is the best choice if the Object contains only primitive values.

7).In Shallow cloning by using main object reference, if we perform any change to the contained object then those changes will be reflected automatically in the cloned copy. To overcome this problem we should go for Deep cloning.

Example:
Test t1=new Test();
Test t2=t1;


Deep Cloning

1). The process of creating an exactly independent duplicate object (including contained objects also) is called deep cloning.

1). In Deep cloning, if the main object contains any reference variable then the corresponding Object copy will also be created in a cloned object.

1). Object class clone( ) method meant for Shallow Cloning, if we want Deep cloning then the programmer is responsible to implement by overriding the clone( ) method.

package com.bala;
class Cat { int j ; Cat(int j) { this.j=j ; } } class Dog implements Cloneable { Cat c ; int i ; Dog(Cat c , int i) { this.c=c ; this.i=i ; } public Object clone( ) throws CloneNotSupportedException { Cat c1=new Cat(c.j) ; Dog d1=new Dog(c1 , i) ; return d1 ; } }
public class DeepClone {
public static void main(String[ ] ar) throws CloneNotSupportedException { Cat c=new Cat(20) ; Dog d1=new Dog(c , 10) ; System.out.println(d1.i +"......"+d1.c.j); // 10......20 Dog d2=(Dog)d1.clone( ) ; d1.i=888 ; d1.c.j=999 ; System.out.println(d2.i +"......"+d2.c.j); // 10......20 } }


5). In Deep cloning by using main Object reference if we perform any change to the contained Object those changes won't be reflected the cloned object.


Test t1=new Test(); Test t2=(Test)t1.clone(); System.out.println(t1==t2); //false System.out.println(t1.hashCode()==t2.hashCode()); //false


Which cloning is best?

1). If the Object contains only primitive variable then Shallow Cloning is the best choice,

2). If the Object contains reference variables then Deep cloning is the best choice.

Cloning by default Shallow cloning.

That's it!!! These are basic differences between Shallow cloning and Deep cloning.