🎓 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
CloneNotSupportedException in Java is a checked exception that occurs when an object's clone() method is called, but the object’s class does not implement the Cloneable interface.
Table of Contents
- What is
CloneNotSupportedException? - Common Causes
- Different Use Cases
- Conclusion
1. What is CloneNotSupportedException?
CloneNotSupportedException indicates that an object cannot be cloned because its class has not implemented the Cloneable interface.
2. Common Causes
- The class does not implement the
Cloneableinterface. - Attempting to clone an object that inherently does not support cloning.
3. Different Use Cases
Use Case 1: Class Not Implementing Cloneable
This example demonstrates CloneNotSupportedException when a class does not implement Cloneable.
class Car {
String model;
Car(String model) {
this.model = model;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class CloneNotSupportedExample {
public static void main(String[] args) {
Car car = new Car("Sedan");
try {
Car clonedCar = (Car) car.clone(); // Throws CloneNotSupportedException
} catch (CloneNotSupportedException e) {
System.out.println("Cloning not supported for Car class.");
}
}
}
Output:
Cloning not supported for Car class.
Use Case 2: Correctly Implementing Cloneable
In this example, the class implements Cloneable to allow cloning without throwing an exception.
class Bike implements Cloneable {
String brand;
Bike(String brand) {
this.brand = brand;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class CloneableExample {
public static void main(String[] args) {
Bike bike = new Bike("Mountain");
try {
Bike clonedBike = (Bike) bike.clone();
System.out.println("Cloned Bike: " + clonedBike.brand);
} catch (CloneNotSupportedException e) {
System.out.println("Cloning not supported.");
}
}
}
Output:
Cloned Bike: Mountain
Use Case 3: Handling CloneNotSupportedException Gracefully
This example shows how to handle CloneNotSupportedException and take appropriate action.
class Tablet {
String model;
Tablet(String model) {
this.model = model;
}
@Override
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException("Cloning not allowed for Tablet class.");
}
}
public class CloneExceptionHandlingExample {
public static void main(String[] args) {
Tablet tablet = new Tablet("Pro");
try {
Tablet clonedTablet = (Tablet) tablet.clone(); // Throws CloneNotSupportedException
} catch (CloneNotSupportedException e) {
System.out.println(e.getMessage());
}
}
}
Output:
Cloning not allowed for Tablet class.
4. Conclusion
CloneNotSupportedException in Java is essential for signaling when an object cannot be cloned. Proper handling and understanding of this exception ensure that cloning is used effectively and safely 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