Object cloning in Java refers to the process of creating an
exact duplicate of an object. To achieve this, Java provides the
clone() method within the Object class.
For an object to be eligible for cloning, its respective class
must implement the Cloneable interface. If Cloneable interface is
not implemented, calling the clone() method will result in a
CloneNotSupportedException.
In Java, the reference to the object is copied, rather than the
object itself. This means that both the original object and the
newly assigned reference point to the same object in memory.
Example of Object cloning with
assignment operator
Java example of Object cloning with assignment operator
// Java example of Object cloning with assignment operator
class Car{
int speed;
public Car() {
// TODO Auto-generated constructor stub
speed=100;
}
}
class Main {
publicstaticvoid main(String[] args){
Car car=new Car();
System.out.println(car.speed);
Car car2=car;
car2.speed=150;
System.out.println(car.speed); //changes made in car2 reflect back to car object
System.out.println(car2.speed);
}
}
Output:
100
150
150
Creating a copy of object using the clone() method
In order to acquire the reference to the duplicated object, any class that
incorporates the clone() method must invoke super.clone().
Additionally, the class needs to implement java.lang.Cloneable. This will result
in the throwing of CloneNotSupportedException if the clone() method is invoked
on an object of that class before generating a clone of the object's Cloneable
interface.
class Vehicles{
int speed;
public Vehicles() {
speed=100;
}
}
class Car implements Cloneable {
int speed;
public Car() {
speed=90;
}
Vehicles vehicles = new Vehicles();
public Object clone() throws CloneNotSupportedException{
returnsuper.clone();
}
}
class Main {
publicstaticvoid main(String[] args) throws CloneNotSupportedException{
Car car=new Car();
Car car2=(Car)car.clone();
car2.vehicles.speed=150; // value of speed is changed to 150
System.out.println(car.speed); // change in speed value is not reflected
System.out.println(car2.vehicles.speed);
}
}
Output:
90
150
Deep Copy and Shallow Copy
Shallow copy : The technique for copying an object that is used by
cloning by default is shallow copy. This method copies the fields of an old
object A to a new object B. The object V will point to the same location as
object A pointed out because the reference is copied to B while copying the
object type field. It copies the value of the primitive type if the field value
is one.
As a result, any modifications made to objects that are references in objects A
or B will be reflected in other objects.
Deep Copy when using the clone() method : If we want to make a deep copy
of object A and put it in a new object B, we must also make a new copy of any
fields that refer to object A and put them in object Y. This means that any
changes made to referenced object fields in object A or B will only affect that
object and not the other.
All fields and the memory that the fields point to that is dynamically allocated
are duplicated during a deep copy. A deep copy happens when an object and the
objects it refers to are both copied.
Advantages of clone
method:
A new copy of the object won't be created if we use the assignment operator to
assign an object reference to another reference variable because it will point
to the same address location as the original object. The original object will
therefore be updated whenever the reference variable changes.
When employing a copy constructor, it's necessary to manually copy all the data,
which entails reassigning all fields of the class within the constructor. On the
other hand, the clone method independently handles all the steps required to
create a new copy. So we use use object cloning to avoide extra processing.
The clone() method provides the quick way for duplicating an object.
Disadvantages of
Object cloning
In order to use the Object.clone() method, we must make several changes to our
code, such as implementing the Cloneable interface, defining the clone() method,
handling the CloneNotSupportedException, and finally calling Object.clone(),
among other things.
Despite the absence of any methods, we must implement a cloneable interface. We
simply need to use it to inform the JVM that our object is capable of being
cloned.
Even in the absence of specific methods, it's necessary to implement a cloneable
interface. This informs the Java Virtual Machine (JVM) that our object can be
cloned. Since the method Object.clone() is protected, we must create our own
clone() method and indirectly invoke Object.clone() within it.
In cases where a subclass requires a clone method, all its parent classes,
including their superclasses, must either internally define the clone() method
or inherit it from another parent class. Failing this, the chain of
super.clone() calls will not work.
It's important to note that Object.clone() only supports shallow copying. If a
more thorough deep cloning is needed, then we must override this method.
It's worth mentioning that Object.clone() doesn't invoke any constructors. As a
result, we have limited control over how an object is instantiated during the
cloning process.
Post your comment