File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassCastException ??

 
Bram Maes
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello fellow ranchers,

I was busy experimenting a bit the protected access modifier when using inheritance. When I run the following I get a ClassCastException and I don't know why.... Can anyone help me ?
Thanks.


SuperClass.java :


SubClass.java :
 
Petrus Pelser
Ranch Hand
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bram Maes:



You can't cast an object of a superclass to the type of a subclass.

The following would be fine:
 
Bram Maes
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stupid mistake... you're right

thanks a lot!
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi above,

You are casting down the inheritance tree. You will only be able to make it pass the compiler but it melts down at run time.

Regards,
Jothi Shankar Kumar. S
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic