aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ClassCastException ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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: 8795

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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: ClassCastException ??
 
Similar Threads
method overriding and visibility promotion.
private constructors
Package Problem...
protected member subclassing
protected constructors