Deep cloning:
Deep cloning refers to creating an independent copy of an object and all the objects it references, recursively copying all levels of the object graph.
This means that any changes made to the original object or its referenced objects will not affect the cloned object.
Deep cloning is typically achieved through a custom implementation.
Here's an example that demonstrates deep cloning:
class MyObject implements Cloneable {
private int value;
public MyObject(int value) {
this.value = value;
}
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class MyClass implements Cloneable {
private int id;
private MyObject myObject;
public MyClass(int id, MyObject myObject) {
this.id = id;
this.myObject = myObject;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public MyObject getMyObject() {
return myObject;
}
@Override
public Object clone() throws CloneNotSupportedException {
MyClass cloned = (MyClass) super.clone();
cloned.myObject = (MyObject) myObject.clone();
// Deep cloning the referenced object
return cloned;
}
}
public class DeepCloningExample {
public static void main(String[] args) {
MyObject originalObject = new MyObject(10);
MyClass original = new MyClass(1, originalObject);
try {
MyClass clone = (MyClass) original.clone();
System.out.println("Original ID: " + original.getId());
System.out.println("Clone ID: " + clone.getId());
clone.setId(2);
clone.getMyObject().setValue(20);
System.out.println("Original ID after modifying clone: " + original.getId());
System.out.println("Clone ID after modifying clone: " + clone.getId());
System.out.println("Original Object value after modifying clone: " + original.getMyObject().getValue());
System.out.println("Clone Object value after modifying clone: " + clone.getMyObject().getValue());
}
catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
In the example above, there are two classes: MyObject
and MyClass
. MyObject
is a simple class that implements the Cloneable
interface and provides a clone()
method that performs a shallow copy.
The MyClass
class contains an id
field of type int
and a myObject
field of type MyObject
.
It also implements the Cloneable
interface and overrides the clone()
method to perform a deep clone.
In the clone()
method, a new instance of MyClass
is created using the superclass's clone()
method, and the referenced MyObject
is deep cloned by invoking its clone()
method.
The main()
method creates an instance of MyObject
and an instance of MyClass
with the original object.
It then clones the MyClass
object, modifies the clone's id
and the value of the referenced MyObject
, and prints the values of the original and cloned objects.
As a result of deep cloning, modifying the clone's id
and the value of the referenced MyObject
does not affect the original object.
The two objects remain independent and have their own copies of the referenced MyObject
.
0 Comments