Introduction
The Cloneable
interface in Java is used to indicate that a class allows its objects to be cloned. This is achieved by implementing the clone()
method from the Object
class.
Table of Contents
- What is
Cloneable
? - Implementing
Cloneable
- Different Use Cases
- Conclusion
1. What is Cloneable?
Cloneable
is a marker interface with no methods. It signals that a class supports cloning, allowing object duplication.
2. Implementing Cloneable
To use Cloneable
, a class must:
- Implement the
Cloneable
interface. - Override the
clone()
method fromObject
.
3. Different Use Cases
Use Case 1: Shallow Cloning
This example demonstrates shallow cloning, where only the object's references are copied.
class Person implements Cloneable {
String name;
Person(String name) {
this.name = name;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "Person{name='" + name + "'}";
}
}
public class ShallowCloneExample {
public static void main(String[] args) {
try {
Person original = new Person("John");
Person cloned = (Person) original.clone();
System.out.println("Original: " + original);
System.out.println("Cloned: " + cloned);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
Output:
Original: Person{name='John'}
Cloned: Person{name='John'}
Use Case 2: Deep Cloning
In this example, deep cloning is performed by manually copying nested objects.
class Address implements Cloneable {
String city;
Address(String city) {
this.city = city;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Employee implements Cloneable {
String name;
Address address;
Employee(String name, Address address) {
this.name = name;
this.address = address;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Employee cloned = (Employee) super.clone();
cloned.address = (Address) address.clone();
return cloned;
}
@Override
public String toString() {
return "Employee{name='" + name + "', address=" + address.city + "}";
}
}
public class DeepCloneExample {
public static void main(String[] args) {
try {
Address address = new Address("New York");
Employee original = new Employee("Alice", address);
Employee cloned = (Employee) original.clone();
System.out.println("Original: " + original);
System.out.println("Cloned: " + cloned);
cloned.address.city = "San Francisco";
System.out.println("After modification:");
System.out.println("Original: " + original);
System.out.println("Cloned: " + cloned);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
Output:
Original: Employee{name='Alice', address=New York}
Cloned: Employee{name='Alice', address=New York}
After modification:
Original: Employee{name='Alice', address=New York}
Cloned: Employee{name='Alice', address=San Francisco}
Use Case 3: Handling Cloning Exceptions
This example demonstrates handling CloneNotSupportedException
when cloning an object.
class Product implements Cloneable {
String name;
Product(String name) {
this.name = name;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class CloneExceptionHandling {
public static void main(String[] args) {
Product product = new Product("Laptop");
try {
Product clonedProduct = (Product) product.clone();
System.out.println("Cloned Product: " + clonedProduct.name);
} catch (CloneNotSupportedException e) {
System.out.println("Cloning not supported.");
}
}
}
Output:
Cloned Product: Laptop
4. Conclusion
The Cloneable
interface in Java enables object cloning, allowing for shallow and deep copies of objects. Proper implementation and handling of the clone()
method are essential for effective use of cloning in Java applications.
Comments
Post a Comment
Leave Comment