🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
Cloneableinterface. - 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment