It is a default cloning in java implemented by the Java vendor simply by using one predefined method in the Object class i.e clone(). Using shallow cloning we can only be able to clone the member of the class but fails to clone object members of the class. To clone the object member we need to write our own code which is also called as Deep Cloning.
To achieve shallow Cloning we need to implement Cloneable interface and need to call the clone() method with the object of the class whose object is to be cloned but it returns the Object class object so need to type-cast to its respective class type.
Example showing shallow cloning :
public class deepCloning implements Cloneable {
int x= 20;
String s="java";
public static void main(String[] args) {
deepCloning d = new deepCloning();
deepCloning d1=null;
try{
d1=(deepCloning)d.clone(); // Cloning statement
}catch(Exception e){
e.printStackTrace();
}
System.out.println(d); // Original Object
System.out.println(d1); // Cloned Object
System.out.println(d.x); // 20 as output
System.out.println(d1.x); // 20 as output
d1.x=30; // Value changed to 30 by
cloned object
System.out.println(d.x); // 20 as output
System.out.println(d1.x);//30 as output(not affecting d)
}
}
In the above code
d = Original Object
d1 = Cloned Object
If we try to change the object d will not reflect on object d1 and vice-versa.
What if we are not implemeting the Cloneable interface ?
If we are not implementing Cloneable then it will Runtime error saying CloneNotSupportedException. So its mandatory to implement Cloneable Interface which is marker interface to indicate JVM that the object of that implementing class is to be cloned.
Example showing how shallow Cloning fails to clone Object member
class F{
int x=30;
String s="java";
}
public class deepCloning implements Cloneable{
int x=40;
String s1="tutorial";
F f;
deepCloning(){
f = new F();
}
public static void main(String[] args) {
deepCloning sc = new deepCloning();
deepCloning sc1=null;
try{
sc1=(deepCloning)sc.clone();//Cloning statement
}catch(Exception e){
e.printStackTrace();
}
System.out.println(sc.f.x); // 30 as output
System.out.println(sc1.f.x); // 30 as output
sc.f.x=60; // changing the x value of f
System.out.println(sc.f.x); // 60 as output
System.out.println(sc1.f.x);// 60 as output
}
}
In the above code even if i tried to change the value of instance variable x of class F through deepCloning class object, the value get changed but by accessing with the cloned object it result the changed value which indicated that cloned object's object member f is also pointing to the same object. To avoid this type of problem we have the concept of deep cloning. We can see the last two line of the above program that result of both line even if in the previous line i modified the value of x to 60.
Diagrammatic representation of the above program
( Above diagram showing the shallow cloning )
What is the problem with the shallow Cloning ?
While Cloning it fails to clone the object member. As we can see in the above example while cloning it simple copies the reference value of the ' f ' to the cloned object which also point the object which was pointing by the original object;s object member.
0 comments:
Post a Comment