This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes downcasting failed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "downcasting failed" Watch "downcasting failed" New topic
Author

downcasting failed

meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Exception in thread "main" java.lang.ClassCastException: A cannot be cast to B
at B.main(B.java:12)

Is there any way to downcast without run-time error ?


My one regret in life is that I am not someone else.
- Woody Allen
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2174
    
  47
Exception in thread "main" java.lang.ClassCastException: A cannot be cast to B
at B.main(B.java:12)

That's because objA references an object of type A which can't possibly be cast to type B.
Where you can do this type of cast is as follows:

This works because whilst objA is of type A it is references an object of type B and so it can be cast back to a type B.
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: downcasting failed
 
Similar Threads
Class cast
!(boolean exp.) and extending
Island of Isolation
Garbage collector vs circular reference
Comparator question