Last Updated: 28 May, 2022
Cloning is the process of creating an exact copy of an existing object in the memory.
For implementing the cloning feature in Java, the clone()
method of a java.lang.Object
class is used to clone an existing object. This method creates an exact copy of an object on which it is called through the field-by-field assignment and returns the reference of that object.
Not all the objects in Java are eligible for the cloning process. The class whose objects we want to clone that class must implement Cloneable Interface. If we do not implement Cloneable interface and try to clone the object, the clone()
method generates CloneNotSupportedException
.
Let's have a look the syntax of Object class clone() method
protected Object clone() throws CloneNotSupportedException
A cloneable interface is a marker interface in Java that is used to provide the marker for the cloning process. A class implements the Cloneable interface to indicate to the Object.clone()
method that it is legal for that method to make a field-for-field copy of instances of that class.
Java mainly supports two types of cloning:
Shallow Cloning is the default cloning process in Java and it is done by invoking the clone() method where a shallow copy of the original object will be created with the exact field. In case the original object has references to some other objects as fields, then only the references of that object will be cloned instead of new object creation. if we change the value of the cloned objects then it will be reflected in the original as well. Thus, shallow cloning is dependent on the original object. A shallow copy is not 100% disjoint from the original object and not 100% independent of the original object.
After the definition of Shallow Cloning let's understand it by this given below example:
Example 1 : Shallow Cloning implementation in Java
Output
Original object (person1) data:
Manoj 25 MG Road Bengaluru Karnataka
Cloned object (person2) data:
Manoj 35 Electronic City Bengaluru Karnataka
Original object (person1) data after modifying cloned object:
Manoj 25 Electronic City Bengaluru Karnataka
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 addition, if the original object has any references to other objects as fields, then copies of those objects are also created by calling the clone()
method on them. That means cloned object and original object will be 100% disjoint. Both the objects will be 100% independent of each other. Any changes made to cloned object will not be reflected in the original object or vice-versa.
To create a deep copy of an object, we have to override the clone()
method as demonstrated in the given below example:
Example 2 : Deep Cloning implementation in Java
Output
Original object (person1) data:
Manoj 25 MG Road Bengaluru Karnataka
Cloned object (person2) data:
Manoj 35 Electronic City Bengaluru Karnataka
Original object (person1) data after modifying cloned object:
Manoj 25 MG Road Bengaluru Karnataka
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
Why clone() method is needed in Java?
The clone() method saves the extra processing task for creating the exact copy of an object whereas if we create an object using a new keyword it will take a lot of processing time to perform the same task that's why cloning is a perfect choice if we need a different same existing object.
What is Shallow copy?
A bitwise copy of an object, where a new object is created and it has the same copy of the values in the original object, is called a Shallow copy. If any of the object fields refer to the other object then in such cases only the reference address is copied.
What is Deep copy?
A bitwise copy of an object, where a new object is created and it has the same copy of the values in the original object, is called a Shallow copy. If any of the object fields refer to the other object then in such cases only the reference address is copied.