• Breaking News

    Saturday, 10 October 2015

    How Cloning Works in java



    In java cloning is process of creating exact copy of an object.

    Java provides clone() method in java.lang.object package.To create clone of any object we have to implement Cloneable interface.

    Method deceleration in Cloneable interface is
        
           protected Object clone() throws CloneNotSupportedException 

    If we want to create clone of object then it is necessary that class implement Cloneable Interface if our class not implement Cloneable interface then CloneNotSupportedException type exception will be thrown.

    When clone method is called then java create new instance of object, copy each field one by one from one object to another and return instance of object.

    java.lang.object provide default implementation of clone method but we can also write our custom implementation.

    Below is example of cloning in java 

    Suppose we have class Car which implement Cloneable Interface

    package com.kodemaker;
    
    public class Car implements Cloneable{
    
     public String carModel;
     public String carColor;
    
     public Car(String carModel, String carColor) {
      super();
      this.carModel = carModel;
      this.carColor = carColor;
     }
    
     @Override
     protected Car clone() throws CloneNotSupportedException {
      return (Car) super.clone();
     }
    
    }
    

    Below is main class by which we create clone of object.

    package com.kodemaker;
    
    public class CloneMain {
        
     public static void main(String[] args) {
         
      Car audiRedCar = new Car("Audi A3 Sedan","Red");
      Car copyAudiRedCar;
      
      try {
       //create clone of object 
       copyAudiRedCar = audiRedCar.clone();
       
       //original object properties
       System.out.println("---audiRedCar Properties---");
       System.out.println("Model- "+audiRedCar.carModel);
       System.out.println("Color- "+audiRedCar.carColor);
       
       //clone object properties
       System.out.println("---copyAudiRedCar Properties---");
       System.out.println("Model- "+copyAudiRedCar.carModel);
       System.out.println("Color- "+copyAudiRedCar.carColor);
       
       // beacause both object has seperate copy so other object not affect if change one object
       System.out.println("---Both object has seperate copy---");
       copyAudiRedCar.carColor = "Blue";
       System.out.println("Color- "+copyAudiRedCar.carColor);
       System.out.println("Color- "+audiRedCar.carColor);
       
      }catch (CloneNotSupportedException e) {
       e.printStackTrace();
      }
      
     }
    }
    



    In above example we can see audiRedCar.clone() != audiRedCar i.e. both object instances are different.
    System.out.println("Color- "+copyAudiRedCar.carColor);
    System.out.println("Color- "+audiRedCar.carColor);

    Output of above programme

    ---audiRedCar Properties---
    Model- Audi A3 Sedan
    Color- Red
    ---copyAudiRedCar Properties---
    Model- Audi A3 Sedan
    Color- Red
    ---Both object has seperate copy---
    Color- Blue
    Color- Red
    

    if Car class not implement Cloneable Interface then

    Exception will be thrown

    java.lang.CloneNotSupportedException: com.kodemaker.Car
     at java.lang.Object.clone(Native Method)
     at com.kodemaker.Car.clone(Car.java:16)
     at com.kodemaker.CloneMain.main(CloneMain.java:12)
    

    If we have not used cloning then we have to create instance of object and copy fields one by one manually which is very time consuming so cloning is better approach for create clone of object

    For more detail of above topic

    you can refer http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

    In next article we learn what is deep cloning and shallow cloning.

    No comments:

    Post a Comment