my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ClassCastException ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ClassCastException ??" Watch "ClassCastException ??" New topic

ClassCastException ??

Bram Maes
Ranch Hand

Joined: Sep 27, 2006
Posts: 32
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. : :

SCJP 5.0, SCEA part I
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132
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

Joined: Sep 27, 2006
Posts: 32
Stupid mistake... you're right

thanks a lot!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10031

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.

Jothi Shankar Kumar. S

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
I agree. Here's the link:
subject: ClassCastException ??
It's not a secret anymore!