This week's book giveaway is in the Agile forum.
We're giving away four copies of The Software Craftsman and have Sandro Mancuso on-line!
See this thread for details.
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

Win a copy of The Software Craftsman this week in the Agile forum!
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: 9763

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 ??