Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

downcasting failed  RSS feed

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Tony Docherty
Bartender
Posts: 3200
78
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!