Javatpoint Logo
Javatpoint Logo

ClassCast Exception in Java

ClassCast Exception in Java is one of the unchecked exceptions that occur when we try to convert one class type object into another class type.

ClassCast Exception is thrown when we try to cast an object of the parent class to the child class object. However, it can also be thrown when we try to convert the objects of two individual classes that don't have any relationship between them.

Consider the following example, in which we have shown how the ClassCast Exception is thrown.

Main.java

Output:

My dog is of black Color
Exception in thread "main" java.lang.ClassCastException: class Animal cannot be cast to class Dog (Animal and Dog are in the unnamed module of loader 'app')
at Main.main(Main.java:12)

Next TopicCloneable in Java




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA