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

Diagram of Shallow Cloning

                                       ( 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. 

Related Posts:

  • Static inner class in Java Like Instance inner class we have also static inner class. Static instance class is loaded at the time of loading the outer class as it is declared as static that means it will act as the static member of the outer class and… Read More
  • Instance Inner class in Java As the name itself indicates that inner class with non-static scope that means a member of outer class which is of instance scope called as instance inner class. Instance inner class is itself a class but we cannot use any s… Read More
  • Access Modifiers in java Access Modifier in java defines the scope of accessibility of methods, constructors, variables etc. So to access the different members of the class/interfaces with different scope we have three access modifiers : 1. Publ… Read More
  • Local inner class in Java Local inner class is a class which is define local to the outer class. So it must be defined within the local scope of a class like method, constructor or initialization block . A class within a class in never treated as the… Read More
  • Anonymous class in Java Like other classes in java , anonymous is also a type of class but it doesn't have any name like other classes have their own name either defined by the user or pre-defined in the java by the java vendors. So anonymous class… Read More

0 comments:

Post a Comment

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201978

Online Members

Live Traffic