Course Content
Core Java
About Lesson

Cloning in Java refers to the process of creating an exact copy of an object. There are two main types of cloning: shallow cloning and deep cloning.

Shallow Cloning

Shallow cloning creates a new object, but it copies the references to the objects contained in the original object. This means that the new object is a copy of the original, but any nested objects inside are not copied; instead, their references are copied.

To implement shallow cloning in Java, you use the clone() method from the Cloneable interface. The class must override the clone() method and handle the CloneNotSupportedException.

class Address implements Cloneable {
    String city;
    String street;

    Address(String city, String street) {
        this.city = city;
        this.street = street;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return city + ", " + street;
    }
}

class Person implements Cloneable {
    String name;
    Address address;

    Person(String name, Address address) {
        this.name = name;
        this.address = address;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return name + " lives at " + address;
    }

    public static void main(String[] args) {
        try {
            Address address = new Address("New York", "5th Avenue");
            Person person1 = new Person("John", address);
            Person person2 = (Person) person1.clone();

            System.out.println("Before modification:");
            System.out.println(person1);
            System.out.println(person2);

            person2.name = "Doe";
            person2.address.street = "Broadway";

            System.out.println("After modification:");
            System.out.println(person1);
            System.out.println(person2);

        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

Deep Cloning

Deep cloning creates a new object and recursively copies all objects referenced by the original object. This means that the cloned object and the original object are completely independent, including any nested objects. It is complex to implement but avoids unintended side effects and ensures true independence between the original and cloned objects.

To implement deep cloning, you need to override the clone() method and manually clone each field of the object, ensuring that any mutable objects are also cloned.

class Address implements Cloneable {
    String city;
    String street;

    Address(String city, String street) {
        this.city = city;
        this.street = street;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return city + ", " + street;
    }
}

class Person implements Cloneable {
    String name;
    Address address;

    Person(String name, Address address) {
        this.name = name;
        this.address = address;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person cloned = (Person) super.clone();
        cloned.address = (Address) address.clone(); // Deep cloning of address
        return cloned;
    }

    @Override
    public String toString() {
        return name + " lives at " + address;
    }

    public static void main(String[] args) {
        try {
            Address address = new Address("New York", "5th Avenue");
            Person person1 = new Person("John", address);
            Person person2 = (Person) person1.clone();

            System.out.println("Before modification:");
            System.out.println(person1);
            System.out.println(person2);

            person2.name = "Doe";
            person2.address.street = "Broadway";

            System.out.println("After modification:");
            System.out.println(person1);
            System.out.println(person2);

        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

Scroll to Top