What do you understand by Object Cloning and how do you achieve it in Java?

By | March 5, 2023

What do you understand by Object Cloning and how do you achieve it in Java?

Object cloning in Java is a process of creating a copy of an existing object. This can be useful when we want to create a new object with the same state as an existing object, without modifying the existing object.

Java provides two ways to achieve object cloning:

                                                      

  1. Shallow cloning: In shallow cloning, a new object is created with the same state as the existing object. However, if the existing object contains any references to other objects, those references are also copied to the new object, so both objects will refer to the same underlying objects.

To perform shallow cloning in Java, the class of the object being cloned must implement the Cloneable interface, and the clone() method must be overridden to create a new object and copy the state of the existing object into it. The default implementation of clone() method in Object class throws CloneNotSupportedException. Here is an example of shallow cloning:

class MyClass implements Cloneable {

    private int value;

    private List<String> myList;

    public MyClass(int value, List<String> myList) {

        this.value = value;

        this.myList = myList;

    }

    @Override

    public Object clone() throws CloneNotSupportedException {

        return super.clone(); // Shallow cloning

    }

}

// Creating a new object using shallow cloning

MyClass obj1 = new MyClass(10, new ArrayList<>());

MyClass obj2 = (MyClass) obj1.clone(); // Shallow cloning

2. Deep cloning: In deep cloning, a new object is created with the same state as the existing object, but any references to other objects are also copied recursively, so that the new object and the existing object refer to different underlying objects.

To perform deep cloning in Java, we need to implement a custom cloning method that copies not only the state of the existing object but also the state of any referenced objects. Here is an example of deep cloning:

class MyClass implements Cloneable {

    private int value;

    private List<String> myList;

 

    public MyClass(int value, List<String> myList) {

        this.value = value;

        this.myList = myList;

    }

    @Override

    public Object clone() throws CloneNotSupportedException {

        MyClass clonedObj = (MyClass) super.clone(); // Shallow cloning

        clonedObj.myList = new ArrayList<>(myList); // Deep cloning

        return clonedObj;

    }

}

// Creating a new object using deep cloning

MyClass obj1 = new MyClass(10, new ArrayList<>());

MyClass obj2 = (MyClass) obj1.clone(); // Deep cloning

Note that in both cases, the class of the object being cloned must implement the Cloneable interface, and the clone() method must be overridden to provide the desired behavior. Additionally, deep cloning can be more complex and less efficient than shallow cloning, especially if the object being cloned has a deep hierarchy of nested objects.

Leave a Reply

Your email address will not be published. Required fields are marked *