Cloneable in JAVA
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.
- package Com.java.Main;
- public class Main {
- public static void main(String[] args) {
- /**
- * Create object
- */
- CloneExample ce=new CloneExample(15, "Object Created");
- try {
- System.out.println(ce.values());
- System.out.println("Making the Clone.");
- CloneExample ce2=(CloneExample) ce.clone();
- System.out.println(ce2.values());
- } catch (CloneNotSupportedException e) {
- e.printStackTrace();
- }
- }
- }
- package Com.java.Main;
- import java.util.*;
- public class CloneExample implements Cloneable{
- int i;
- String s;
- //parameterize constructor
- public CloneExample(int i,String s) {
- this.i=i;
- this.s=s;
- }
- /**
- * Clone method override
- */
- public Object clone() throws CloneNotSupportedException {
- return super.clone();
- }
- // Metheod to return values
- public Map<String,Object> values(){
- Map<String,Object> map=new HashMap<String,Object>();
- map.put("String Value", this.s);
- map.put("Integer Value", this.i);
- return map;
- }
- }
Advantage of cloning
- It is still a popular and easy way of copying objects.
- You don't need to write lengthy and repetitive codes. Just use an abstract class with a 4- or 5-line long clone() method.
- 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.
- Clone() is the fastest way to copy the array.
The disadvantage of cloning
- 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.
- 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.
- Object.clone() is protected, so we have to provide our own clone() and indirectly call Object.clone() from it.
- Object.clone() doesn't invoke any constructor so we don't have any control over object construction.
- 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.
- 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.