100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is the clone() Method?

What clone() does in Java — Cloneable, super.clone(), shallow copy defaults and safe overriding — with a worked example and interview Q&A.

mediumQ103 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

clone() is a protected method defined on Java’s Object class that creates and returns a field-by-field copy of the calling object, and a class must implement the Cloneable marker interface and override clone() as public before it can be safely called from outside the class.

Calling clone() without implementing Cloneable throws a CloneNotSupportedException, because Cloneable itself declares no methods — it only signals to Object.clone() that field-by-field copying is permitted for that class. The default implementation performs a shallow copy: primitive fields are duplicated by value, but reference fields still point at the same nested objects as the original. Classes with mutable reference fields must override clone(), call super.clone(), and then explicitly deep-copy those fields to avoid unintended aliasing. Many modern codebases avoid clone() entirely in favor of copy constructors or static factory methods, since clone()’s API is widely considered a design mistake due to its checked exception and easy-to-misuse contract.

  • Provides a built-in way to duplicate an object’s state
  • super.clone() handles primitive field copying automatically
  • Overriding it lets a class control exactly what “copying” means
  • Cloneable acts as an explicit opt-in safety signal

AI Mentor Explanation

A groundskeeper can only duplicate the pitch-preparation blueprint if the club has explicitly registered that pitch as “clonable” in the facilities system — attempting to copy an unregistered pitch’s blueprint throws it back as a rejected request. Once registered, the default duplication just copies the blueprint’s basic measurements field by field, but if the blueprint references a specific soil-mix supplier, the groundskeeper must explicitly re-source new soil for the clone rather than share the original supplier link. clone() works the same way: it requires an explicit opt-in (Cloneable) and a default shallow copy that the class must extend for anything nested.

Step-by-Step Explanation

  1. Step 1

    Implement Cloneable

    The class must implement the Cloneable marker interface to opt in; otherwise clone() throws CloneNotSupportedException.

  2. Step 2

    Override clone() as public

    Object’s clone() is protected; override it and widen visibility to public so external callers can use it.

  3. Step 3

    Call super.clone()

    Delegate to Object.clone() to get the default shallow, field-by-field copy.

  4. Step 4

    Deep-copy mutable references

    Explicitly clone or rebuild any mutable reference fields so the copy doesn’t alias the original’s nested state.

What Interviewer Expects

  • Knowledge that Cloneable is a marker interface with no methods
  • Awareness that clone() throws CloneNotSupportedException without Cloneable
  • Understanding that super.clone() gives a shallow copy by default
  • Ability to explain why many teams prefer copy constructors over clone()

Common Mistakes

  • Thinking Cloneable declares a clone() method that must be implemented
  • Forgetting clone() is protected in Object and must be widened to public
  • Not calling super.clone() and instead manually reconstructing every field
  • Assuming clone() always produces a deep copy

Best Answer (HR Friendly)

clone() is a method every Java object technically has, but it only works safely if the class explicitly opts in by implementing Cloneable and overriding clone() to be public. By default it copies fields shallowly, so if the class holds references to other mutable objects, I have to manually deep-copy those inside my override. A lot of teams actually avoid clone() altogether and use a copy constructor instead, since clone()’s contract is easy to get wrong.

Code Example

Overriding clone() with a deep-copied field
class Address {
    String city;
    Address(String city) { this.city = city; }
    Address copy() { return new Address(this.city); }
}

class Person implements Cloneable {
    String name;
    Address address;

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

    @Override
    public Person clone() {
        try {
            Person cloned = (Person) super.clone(); // shallow copy first
            cloned.address = this.address.copy();   // then deep-copy the reference
            return cloned;
        } catch (CloneNotSupportedException e) {
            throw new AssertionError(e); // won’t happen: we implement Cloneable
        }
    }
}

Person p1 = new Person("Amy", new Address("Austin"));
Person p2 = p1.clone();
p2.address.city = "Denver";
// p1.address.city is still "Austin" because address was deep-copied

Follow-up Questions

  • Why is Cloneable considered a “marker” interface?
  • What exception does clone() throw and when?
  • Why do many style guides recommend copy constructors over clone()?
  • How would you clone a class whose superclass does not implement Cloneable?

MCQ Practice

1. What does the Cloneable interface declare?

Cloneable declares no methods; it only signals to Object.clone() that copying is permitted.

2. Calling clone() on a class that does not implement Cloneable results in?

Without implementing Cloneable, Object.clone() throws CloneNotSupportedException at runtime.

3. super.clone() inside an overridden clone() method produces?

super.clone() delegates to Object’s default shallow copy; deep copying of references must be added manually.

Flash Cards

clone() defined where?On java.lang.Object, as a protected method.

Cloneable is a?Marker interface with no methods — it only enables clone() usage.

Default clone() behavior?Shallow copy — reference fields are shared, not duplicated.

Common alternative to clone()?A copy constructor or a static factory “of”/"copyOf" method.

1 / 4

Continue Learning