This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft 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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ClassCastException ??" Watch "ClassCastException ??" New topic
Author

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.


SuperClass.java :


SubClass.java :


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: 9243
    
    1

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


SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassCastException ??
 
Similar Threads
private constructors
Package Problem...
protected member subclassing
method overriding and visibility promotion.
protected constructors