What is the difference between shallow and deep cloning in the Prototype pattern?

In the Prototype pattern, shallow cloning copies an object's field values but leaves reference-type fields pointing at the same shared objects as the original, while deep cloning also copies those referenced objects so the clone is fully independent.

Key Points

Example

If a Person object has a shallow-cloned Address field, changing the cloned person's address.city also changes it for the original person, because both objects still point at the same Address instance; a deep clone would give the copy its own separate Address object.

public class ShallowVsDeepClone {

    static class Address {
        String city;
        Address(String city) { this.city = city; }
    }

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

        Person shallowCopy() { return new Person(name, address); }                 // shares Address
        Person deepCopy() { return new Person(name, new Address(address.city)); }  // own Address
    }

    public static void main(String[] args) {
        Person original = new Person("Asha", new Address("Pune"));

        Person shallow = original.shallowCopy();
        shallow.address.city = "Mumbai";
        System.out.println("After shallow copy change, original city = " + original.address.city);

        original.address.city = "Pune";
        Person deep = original.deepCopy();
        deep.address.city = "Delhi";
        System.out.println("After deep copy change, original city = " + original.address.city);
    }
}

Output

After shallow copy change, original city = Mumbai

After deep copy change, original city = Pune

Interview Tip

A concise interview answer is

Shallow cloning copies field values but leaves object references shared between the original and the clone, so mutating a referenced object affects both. Deep cloning recursively copies those referenced objects too, giving you a fully independent copy — Java's default clone() is shallow, so deep copying needs to be implemented explicitly.

Last updated: 1 November 2026

Previous question: What is the Prototype pattern and how does it work?

Next question: How do you implement the Prototype pattern in Java?

More Java Design Patterns interview questions

All Java Design Patterns questions