ClassCastException
is a common runtime exception in Java that occurs when you try to cast an object to a subclass or class that it is not an instance of. This exception is a subclass of RuntimeException
, which means it is unchecked and typically indicates a logic error in the program.
When Does ClassCastException
Occur?
ClassCastException
occurs when:
- You cast an object of one type to another type which it is not a subclass or superclass of.
- The cast is performed incorrectly, often due to poor understanding of the object type hierarchy.
Syntax
ClassCastException classCastException = new ClassCastException("message");
Common Scenarios
- Incorrect Downcasting:
Trying to downcast a superclass to a subclass when the object is not actually an instance of the subclass. - Incorrect Use of Generics:
Misusing generics can lead to incorrect casting and thusClassCastException
.
Example 1: Incorrect Downcasting
Here, we attempt to cast a Parent
type object to a Child
type object, which leads to a ClassCastException
.
class Parent {
void show() {
System.out.println("Parent show method");
}
}
class Child extends Parent {
void display() {
System.out.println("Child display method");
}
}
public class Main {
public static void main(String[] args) {
Parent parent = new Parent();
// This cast will throw ClassCastException at runtime
Child child = (Child) parent;
child.display();
}
}
Output
Exception in thread "main" java.lang.ClassCastException: Parent cannot be cast to Child
Example 2: Incorrect Use of Generics
Here, we misuse generics by casting an object to an incompatible type.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> integerList = new ArrayList<>();
integerList.add(1);
integerList.add(2);
integerList.add(3);
// This cast will throw ClassCastException at runtime
ArrayList<String> stringList = (ArrayList<String>) (ArrayList<?>) integerList;
for (String s : stringList) {
System.out.println(s);
}
}
}
Output
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
Avoiding ClassCastException
To avoid ClassCastException
, follow these practices:
- Use the
instanceof
Operator: Before casting, always check if the object is an instance of the target type.
if (parent instanceof Child) {
Child child = (Child) parent;
child.display();
} else {
System.out.println("parent is not an instance of Child");
}
- Generics Usage: Use generics to ensure type safety at compile time, which helps to avoid casting issues at runtime.
ArrayList<String> stringList = new ArrayList<>();
stringList.add("Hello");
stringList.add("World");
// This will cause compile-time error if types are incompatible
// Hence, preventing ClassCastException at runtime
for (String s : stringList) {
System.out.println(s);
}
- Proper Type Hierarchy Understanding: Make sure you understand the type hierarchy of the classes you are working with to avoid incorrect casting.
Conclusion
ClassCastException
is a runtime exception that occurs due to incorrect type casting. By using proper type checking with instanceof
, correctly utilizing generics, and understanding the type hierarchy, you can avoid this exception and write more robust Java programs.