Cloneable Interface in Java
In Java, the Cloneable interface is a marker interface that indicates that a class is capable of being cloned. Cloning refers to the process of creating an exact copy of an existing object. By implementing the Cloneable interface, a class specifies that it supports cloning and allows objects of that class to be cloned. However, it's important to note that the Cloneable interface itself does not provide any methods. It acts as a signal to the Java runtime environment that an object can be cloned using the clone() method, which is defined in the Object class.
When a class implements Cloneable, it indicates that it has considered the implications of cloning and has implemented the necessary logic to create a valid copy of itself. By convention, the clone() method should be overridden in the class to provide a meaningful implementation of cloning. Implementing this interface and providing a custom clone() method allows you to control how the object is duplicated, ensuring the desired behaviour for your specific class.
Example
FileName: MyClass.java
// Define a class that implements the Cloneable interface public class MyClass implements Cloneable { private int value; // Private member variable to store an integer value private String name; // Private member variable to store a String name // Constructor to initialize the object with a value and name public MyClass(int value, String name) { this.value = value; this.name = name; } // Setter method to update the value public void setValue(int value) { this.value = value; } // Getter method to retrieve the value public int getValue() { return value; } // Setter method to update the name public void setName(String name) { this.name = name; } // Getter method to retrieve the name public String getName() { return name; } // Method to display the current value and name public void display() { System.out.println("Value: " + value); System.out.println("Name: " + name); } // Override the clone() method from the Cloneable interface @Override public Object clone() throws CloneNotSupportedException { return super.clone(); // Call the clone() method of the Object class } // Main method to demonstrate the cloning functionality public static void main(String[] args) { // Create an instance of MyClass MyClass obj1 = new MyClass(7, "Ram"); try { // Create a copy of obj1 using the clone() method MyClass obj2 = (MyClass) obj1.clone(); // Verify that obj2 is a copy of obj1 System.out.println("-- Original Object --"); obj1.display(); System.out.println("-- Cloned Object --"); obj2.display(); // Modify obj2 and verify that obj1 remains unchanged obj2.setValue(14); obj2.setName("Sham"); System.out.println("-- Original Object after modification --"); obj1.display(); System.out.println("-- Cloned Object after modification --"); obj2.display(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } }
Output:
-- Original Object – Value: 7 Name: Ram -- Cloned Object -- Value: 7 Name: Ram -- Original Object after modification -- Value: 7 Name: Ram -- Cloned Object after modification -- Value: 14 Name: Sham