• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassCastException ??

 
Bram Maes
Ranch Hand
Posts: 32
  • 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
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stupid mistake... you're right

thanks a lot!
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic