- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is a ClassCastException and when it will be thrown in Java?n
The java.lang.ClassCastException is one of the unchecked exception in Java. It can occur in our program when we tried to convert an object of one class type into an object of another class type.
When will be ClassCastException is thrown
- When we try to cast an object of Parent class to its Child class type, this exception will be thrown.
- When we try to cast an object of one class into another class type that has not extended the other class or they don't have any relationship between them.
Example
class ParentTest { String parentName; ParentTest(String n1){ parentName = n1; } public void display() { System.out.println(parentName); } } class ChildTest extends ParentTest { String childName; ChildTest(String n2) { super(n2); childName = n2; } public void display() { System.out.println(childName); } } public class Test { public static void main(String args[]) { ChildTest ct1 = new ChildTest("Jai"); ParentTest pt1 = new ParentTest("Adithya"); pt1 = ct1; pt1.display(); ParentTest pt2 = new ParentTest("Sai"); ChildTest ct2 = (ChildTest)pt2; } }
Output
Jai Exception in thread "main" java.lang.ClassCastException: ParentTest cannot be cast to ChildTest at Test.main(Test.java:30)
- Related Articles
- What is a ClassCastException and when it will be thrown in Java?
- When will be an IllegalStateException (unchecked) thrown in Java?
- When does a NullPointerException get thrown in java?
- When do IllegalStateException and IllegalArgumentException get thrown? in java?
- If the weight of a body on the earth is $6 N$, what will it be on the moon?
- What is plasmolysis? What will happen to a plasmolysed cell when it is placed in water?
- An object weighs 10 N in air. When immersed fully in a liquid, it weighs only 8 N. The weight of liquid displaced by the object will be:(a) 2 N(b) 8 N(c) 10 N(d) 12 N
- What is Scanner class in Java? when was it introduced?
- What is no-confidence motion? When can it be held?
- What will happen if we cover the utensil with a poly bag when the water would be boiling and when it starts the evaporation process?
- An object weighs $10 N$ in air. When immersed fully in water, it weighs only $8 N$. The weight of the liquid displaced by the object will be$(a). 2 N$$(b). 8 N$$(c). 10 N$$(d). 12 N$
- How can an exception be thrown manually by a programmer in java?
- A body is moving along a circular path of radius r. What will be the distance travelled and displacement of the body when it completes half a revolution ?
- What is training? When and why it is needed?
- Which of the following statements is true about the reciprocal of a proper fraction? a)It will always be an improper fraction.b)It will always be a proper fraction.c)It will always be a whole number.d)It will depend on the values of the numerator and the denominator of the fraction.
- When metals react with water, what will be the product?

Advertisements