The act of making an exact duplicate of an object is referred to as object cloning. It makes a brand-new instance of the current object's class and fills all of its fields exactly with the data found in the corresponding row or column of the current object. In this article, we will talk about cloning in Java and detail it along with the clone() method in Java. We will also discuss the advantages and disadvantages of the clone() method and learn how to create a copy of an item using clone() in java.
An exact duplicate of an object can be made by using object cloning. An object can be duplicated using the Object class's clone() function.
For the class with the object for which we wish to clone, we must implement a Cloneable interface for that. CloneNotSupportedException is produced by the clone() method if the Cloneable interface is not implemented.
The clone() method eliminates the need for additional processing to make an exact duplicate of an object. We utilize object cloning since performing it via the new keyword would need a substantial portion of processing time.
The Object class contains a definition for the clone() method. The clone() method has the following syntax:
Wish to make a career in the world of Java? Start with HKR'S Java Training
Let us see a few properties of the clone() method:
Let us see a Java code below to understand the working of clone() method in Java in a better manner:
class Main implements Cloneable {
String name;
int version;
public static void main(String[] args) {
Main object1 = new Main();
object1.name = "iPhone";
object1.version = 13;
System.out.println(object1.name);
System.out.println(object1.version);
try {
Main object2 = (Main)object1.clone();
System.out.println(object2.name);
System.out.println(object2.version);
}
catch (Exception x) {
System.out.println(x);
}
}
}
The output of this Java code will be:
iPhone
13
iPhone
13
There must be a public clone() method in the class being copied, or with one of its root classes.
The clone method's default implementation will create a shallow copy of a source object, which means it creates a new instance of the Object type, replicates all the fields to it, and then returns a fresh object of the same type. This Object must be specifically typecast into the source object's object type.
All of the fields from the source object, along with the primitive class as well as the object references, will be identically duplicated in this object. A duplicate of those items is not made if the parent class contains any links to certain other objects in the field; instead, the new instance will only include links to those objects. This means that any changes we make to the shallow copy will also be applied to the source object. Both occurrences are interconnected.
Let us see an example below to understand shallow copy in a better manner
class HKRclone
{
int x = 30;
}
public class ShallowCopy
{
public static void main(String argvs[])
{
HKRclone object1 = new HKRclone();
HKRclone object2 = object1;
object2.x = 4;
System.out.println("The value of x will be: " + object1.x);
}
}
The output of this Java code will be:
The value of x will be 4
We can claim that we have performed a deep copy when we duplicate an entity to make two or more identical copies which don't reflect modifications made to one entity. The other entities' memory is allocated fresh during a deep copy, and references to those other entities are not duplicated. Everything has a unique, independent reference.
Just like a shallow copy, a deep copy is also an exact replica of an object having every field of the source object, but unlike a shallow copy, if the source object contains any references to other objects as fields, the clone method must be called in order to generate a copy of the object. This demonstrates the independence of the source and destination objects. Any modification to the cloned object has no effect on the original object.
Let us see an example of deep copy to understand it in a better manner.
class HKRclone
{
int x = 20;
}
public class DeepCopy
{
public static void main(String argvs[])
{
HKRclone object1 = new HKRclone();
HKRclone object2 = new HKRclone();
object2.x = 6;
System.out.println("The value of x will be: " + object1.x);
}
}
The output of this java code will be:
The value of x will be 20
Wish to make a career in the world of Full Stack Web Development ? Start with HKR'S Full Stack Web Development Course
Top 30 frequently asked JAVA Interview Questions !
Conclusion
In this article, we have talked about the clone() method in Java. An exact duplicate of an object can be made by using object cloning. An object can be duplicated using the Object class's clone() function. We have also discussed the properties of the clone() method along with the advantages and disadvantages of cloning in Java. We have also put some light on various clone() methods such as deep copy as well as a shallow copy.
Related articles:
Batch starts on 5th Jun 2023, Weekday batch
Batch starts on 9th Jun 2023, Fast Track batch
Batch starts on 13th Jun 2023, Weekday batch
An exact duplicate of an object can be made by using object cloning. An object can be duplicated using the Object class's clone() method.
It is generally called a public method in java.
It is done using CopyOf() method in Java.
Clone means to make a new object based on an existing one. Copying is the process of moving information from one thing to another
Since it is not specified in the Cloneable interface, the clone() method is protected. The clone function is rendered relatively worthless as a result, as it may create clones of your current data.