ClassCast Exception in JavaClassCast 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 |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India