ClassCast Exception in JavaLast Updated : 17 Mar 2025 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 TopicMultiset-interface-in-java |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India