Importance of clone() method in Java?


The clone() method is used to create a copy of an object of a class which implements Cloneable interface. By default, it does field-by-field copy as the Object class doesn't have any idea about the members of the particular class whose objects call this method. So, if the class has only primitive data type members then a new copy of the object will be created and the reference to the new object copy will be returned. But, if the class contains members of any class type then only the object references to those members are copied and hence the member references in both the original object as well as the cloned object refer to the same object.

We will get a CloneNotSupportedException if we try to call the clone() method on an object of a class that doesn't implement the Cloneable interface. This interface is a marker interface and the implementation of this interface simply indicates that the Object.clone() method can be called on the objects of the implementing class.

Syntax

protected Object clone() throws CloneNotSupportedException

We can implement the clone() method in two ways

Shallow Copy

This is a result of the default cloning functionality provided by the Object.clone() method if the class has non-primitive data type members as well. In the case of Shallow Copy, the cloned object also refers to the same object to which the original object refers as only the object references get copied and not the referred objects.

Example

public class ShallowCopyTest {
   public static void main(String args[]) {
      A a1 = new A();
      A a2 = (A) a1.clone();
      a1.sb.append("Tutorialspoint!");
      System.out.println(a1);
      System.out.println(a2);
   }
}
class A implements Cloneable {
   public StringBuffer sb = new StringBuffer("Welcome to ");
      public String toString() {
         return sb.toString();
      }
   public Object clone() {
      try {
         return super.clone();
      } catch(CloneNotSupportedException e) { 
      }
      return null;
   }
}

Output

Welcome to Tutorialspoint!
Welcome to Tutorialspoint!


Deep Copy

We need to override the clone() method for the classes having non-primitive type members to achieve Deep Copy as it requires the member objects to be cloned as well, which is not done by the default cloning mechanism.

Example

public class DeepCopyTest {
   public static void main(String args[]) {
      A a1 = new A();
      A a2 = (A) a1.clone();
      a1.sb.append(" TutorialsPoint!");
      System.out.println(a1);
      System.out.println(a2);
   }
}
class A implements Cloneable {
   public StringBuffer sb = new StringBuffer("Welcome to ");
   public String toString() {
      return sb.toString();
   }
   public Object clone() {
      try {
         A a = (A) super.clone();
         a.sb = new StringBuffer(sb.toString());
         return a;
      }
      catch(CloneNotSupportedException e) { 
      }
      return null;
   }
}

Output

Welcome to TutorialsPoint!
Welcome to
raja
Published on 02-Aug-2019 20:00:11
Advertisements