Cloneable in JAVA - Study24x7
Social learning Network

Welcome Back

Get a free Account today !

or

Cloneable in JAVA

Updated on 15 February 2020
study24x7
javalearning
17 min read 2 views
Updated on 15 February 2020

Cloneable is a marker interface. 

As the name suggests cloneable, it is used for object cloning. The object cloning is a way to create an exact copy of an object. 

The cloneable interface is present in java.lang package. There is a method clone() in the Object class. A class that implements the Cloneable interface indicates that it is legal for clone() method to make a field-for-field copy of instances of that class.

Calling the clone method on an instance of the class who does not implement the cloneable interface gives an exception CloneNotSupportedException.

For the making clone of a class object, the class must implement the cloneable interface and override the method clone.




Why use the clone() method......?


The clone() method saves the extra processing task for creating the exact copy of an object. If we perform it by using the new keyword, it will take a lot of processing time to be performed that is why we use object cloning.


  1. package Com.java.Main;
  2. public class Main {
  3. public static void main(String[] args) {
  4. /**
  5. * Create object
  6. */
  7. CloneExample ce=new CloneExample(15, "Object Created");
  8. try {
  9. System.out.println(ce.values());
  10. System.out.println("Making the Clone.");
  11. CloneExample ce2=(CloneExample) ce.clone();
  12. System.out.println(ce2.values());
  13. } catch (CloneNotSupportedException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. }


  1. package Com.java.Main;
  2. import java.util.*;
  3. public class CloneExample implements Cloneable{
  4. int i;
  5. String s;

  6. //parameterize constructor
  7. public CloneExample(int i,String s) {
  8. this.i=i;
  9. this.s=s;
  10. }
  11. /**
  12. *  Clone method override
  13. */
  14. public Object clone() throws CloneNotSupportedException {
  15. return super.clone();
  16. }

  17. // Metheod to return values
  18. public Map<String,Object> values(){
  19. Map<String,Object> map=new HashMap<String,Object>();
  20. map.put("String Value", this.s);
  21. map.put("Integer Value", this.i);
  22. return map;
  23. }
  24. }


Advantage of cloning


  1. It is still a popular and easy way of copying objects.
  2. You don't need to write lengthy and repetitive codes. Just use an abstract class with a 4- or 5-line long clone() method.
  3. It is the easiest and most efficient way of copying objects, especially if we are applying it to an already developed or an old project. Just define a parent class, implement Cloneable in it, provide the definition of the clone() method and the task will be done.
  4. Clone() is the fastest way to copy the array.


The disadvantage of cloning


  1. To use the Object.clone() method, we have to change a lot of syntaxes to our code, like implementing a Cloneable interface, defining the clone() method and handling CloneNotSupportedException, and finally, calling Object.clone() etc.
  2. We have to implement cloneable interface while it doesn't have any methods in it. We just have to use it to tell the JVM that we can perform clone() on our object.
  3. Object.clone() is protected, so we have to provide our own clone() and indirectly call Object.clone() from it.
  4. Object.clone() doesn't invoke any constructor so we don't have any control over object construction.
  5. If you want to write a clone method in a child class then all of its superclasses should define the clone() method in them or inherit it from another parent class. Otherwise, the super.clone() chain will fail.
  6. Object.clone() supports only shallow copying but we will need to override it if we need deep cloning.


Shallow Copy In Java


The default version of clone() method creates the shallow copy of an object. The shallow copy of an object will have an exact copy of all the fields of the original object. If the original object has any references to other objects as fields, then only references of those objects are copied into clone object, copy of those objects are not created. That means any changes made to those objects through clone objects will be reflected in the original object or vice-versa. Shallow copy is not 100% disjoint from the original object. Shallow copy is not 100% independent of the original object.


Deep Copy In Java


A deep copy of an object will have an exact copy of all the fields of the original object just like a shallow copy. But in additional, if the original object has any references to other objects as fields, then a copy of those objects is also created by calling clone() method on them. That means the clone object and the original object will be 100% disjoint. They will be 100% independent of each other. Any changes made to the clone object will not be reflected in the original object or vice-versa.


study24x7
Write a comment...