The Cloneable interface in Java indicates that a class supports cloning through the Object class's clone() method. It belongs to the java.lang package and is used to create a copy of an existing object.
In this chapter, you will learn about the Cloneable interface, the clone() method, how to clone objects in Java, and the limitations of object cloning.
Cloneable is a marker interface in Java. It does not declare any methods; instead, it indicates that an object's class permits cloning through the clone() method inherited from the Object class.
The Object class provides the clone() method, which creates a shallow copy of an object when cloning is supported. If a class does not implement Cloneable, calling super.clone() throws a CloneNotSupportedException.
To use the default cloning mechanism, a class typically implements Cloneable and overrides clone() with an appropriate access level.
The clone() method is declared in the Object class as follows:
protected native Object clone() throws CloneNotSupportedException;
The method creates and returns a copy of the object when cloning is supported. Because the method is declared protected, a class commonly overrides it to make cloning accessible to its callers.
The following example demonstrates how to implement the Cloneable interface and create a copy of a Student object using the clone() method.
Java
class Student implements Cloneable {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public Student clone() throws CloneNotSupportedException {
return (Student) super.clone();
}
public static void main(String[] args) {
Student s = new Student(101, "John");
System.out.println(s.id + " " + s.name);
try {
Student s1 = s.clone();
System.out.println(s1.id + " " + s1.name);
System.out.println("Are both objects the same? " + (s == s1));
} catch (CloneNotSupportedException e) {
System.out.println("Unable to clone the Student object.");
}
}
}
Output:
101 John 101 John Are both objects the same? false
In this example, the Student class implements Cloneable and overrides the clone() method. The call to super.clone() creates a shallow copy of the object. Both objects contain the same field values, but s and s1 refer to different objects.
The following steps explain how object cloning works in Java.
Java object cloning is commonly discussed in terms of shallow copying and deep copying.
A shallow copy creates a new object and copies the values of its fields. Primitive fields receive their values, while reference fields continue to refer to the same objects as the original.
The default Object.clone() mechanism performs a shallow copy.
A deep copy creates a new object and also creates independent copies of the mutable objects referenced by its fields, as required by the class's design. This usually requires additional implementation beyond calling super.clone().
Deep copying is useful when the original and copied objects must not share mutable nested objects.
The Cloneable interface and clone() method provide the following benefits:
Cloning has some limitations that developers should consider.
Instead of using Cloneable, developers can use other approaches depending on their requirements.
A copy constructor is often easier to understand and maintain because the copying behavior is explicit.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India