Open In App

Cloneable Interface in Java

Last Updated : 08 Oct, 2025
Comments
Improve
Suggest changes
12 Likes
Like
Report

In Java, the Cloneable interface is a marker interface used to indicate that a class allows creating an exact copy of its objects. It is part of java.lang package and is primarily used with the Object.clone() method to create duplicates of objects.

  • It does not contain any methods (Marker Interface).
  • Its main purpose is to signal to the Object.clone() method that the objects of this class can be safely cloned.
  • If a class implements Cloneable, calling clone() on its objects will create a shallow copy.

Syntax

class ClassName implements Cloneable {

// class fields and methods

}

Example 1: Basic Shallow Cloning

This example shows how to create a shallow copy using the Cloneable interface and clone() method, we achieve shallow cloning by simply calling super.clone().

Java
// Implementing Cloneable interface to allow object cloning
class Person implements Cloneable {
    
    String name;
    int age;

    // Constructor to initialize object fields
    Person(String name, int age){
        this.name = name;
        this.age = age;
    }

    // Overriding clone() by simply calling super.clone()
    // enables shallow copy using Object's clone method
    @Override
    protected Object clone() throws CloneNotSupportedException {
        
        return super.clone();
    }
}

public class Geeks {
    public static void main(String[] args) {
        
        try {
            
            // Create original object
            Person p1 = new Person("Alice", 25);

            // Clone the original object
            Person p2 = (Person) p1.clone();

            // Display original and cloned object details
            System.out.println("Original: " + p1.name + ", " + p1.age);
            System.out.println("Clone: " + p2.name + ", " + p2.age);

            // Modify the clone to show both objects are separate
            p2.name = "Bob";

            System.out.println("After modification:");
            
            // Original remains unchanged
            System.out.println("Original: " + p1.name); 
            
            // Clone has new value
            System.out.println("Clone: " + p2.name);    
        } 
        catch (CloneNotSupportedException e) 
        {
            // Handle exception if object cloning is not supported
            e.printStackTrace();
        }
    }
}

Output
Original: Alice, 25
Clone: Alice, 25
After modification:
Original: Alice
Clone: Bob

Explanation:

  • The clone() method creates a copy of the object.
  • Modifying the clone does not affect the original object because each object has its own memory.
  • Cloneable is a marker interface that signals Object.clone() can safely copy the object.

Example 2: Deep Copy Example

This example demonstrates a deep copy in Java, where the object and its nested objects are fully duplicated, so changes to the clone do not affect the original.

Java
// Address class used as a nested object
class Address {
    String city;
    Address(String city) {
        this.city = city;
    }

    // Copy constructor for deep copying
    Address(Address addr) {
        this.city = addr.city;
    }
}

// Person class implementing deep cloning
class Person implements Cloneable{
    
    String name;
    Address address;

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

    // Overriding clone() for deep copy
    @Override
    protected Object clone() throws CloneNotSupportedException{
        
        // Create new Address object to achieve deep copy
        Person cloned = (Person) super.clone();
        cloned.address = new Address(this.address);
        return cloned;
    }
}

public class DeepCloneExample{
    
    public static void main(String[] args){
        
        try {
            // Create original object
            Person p1 = new Person("Alice", new Address("New York"));

            // Clone original object
            Person p2 = (Person) p1.clone();

            // Modify clone’s nested object
            p2.address.city = "London";

            System.out.println("Original City: " + p1.address.city);
            System.out.println("Clone City: " + p2.address.city);
        } 
        catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

Output
Original City: New York
Clone City: London

Advantages of Using Cloneable

  • Easy Object Copying: Simplifies creation of object copies.
  • Improves Performance: Faster than manually creating a new object and copying all fields.
  • Customizable Cloning: Can implement shallow or deep cloning as needed.
  • Useful in Prototyping: Quickly generate copies of objects without reinitializing them.

Related Article

Shallow vs deep copy



Explore