aspose file tools*
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 Spring in Action this week in the Spring 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: 2316
    
  49
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