File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Run time casting of Objects 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 "Run time casting of Objects" Watch "Run time casting of Objects" New topic

Run time casting of Objects

s khosa
Ranch Hand

Joined: May 15, 2003
Posts: 72
Could some one explain to me why this piece of code will throw RunTimeException, eventhough we are doing an Explicit cast from a parent object to a child object within the valid hierarchy tree:
class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class CEx{
public static void main(String argv[]){
Base b=new Base();
Sub s=(Sub) b;
Kaz Yosh
Ranch Hand

Joined: May 22, 2003
Posts: 63
I believe Down casting cause runtime exception when Sub class has some new method which Super class doesnt. but in your example, there's no method implemented at all so should be ok. I dont know why.

"If anything can go wrong, it will"
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
If an object is an instance of a base class then it can not be cast to a subclass for the reason stated earlier by Kaz. The rule applies even if the subclass does not declare any new members.

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="" target="_blank" rel="nofollow">Try my mock exam.</a>
mohd liyakhat ali

Joined: May 12, 2003
Posts: 2
Directly we cant downcast the super-class

object to sub-class.
First of all we should upcast the sub-class

object to super-class...
then downcast from super-class to sub-class
Stanislava Trajlov

Joined: May 15, 2003
Posts: 15
I was confused with the same mock exam question.
An exerpt from Java 1.4 Certification book (pg 123, Object Reference Casting section):
"...The rules for casting are broader than those for conversion. Some of these rules concern reference type and can be enforced by the compiler at compile time; other rules concern class and can be enforced only during execution.
When both OLDTYPE and NEWTYPE are classes, one class must be a subclass of the other."
It is the case we have here (Sub class is a subclass of Base class).
In the same section:
"Assuming that a desired cast survives compilation, a second check must occur at RUNTIME. The second check determines whether the class of the object being cast is compatible with the new type...
Here, COMPATIBLE means that the class can be converted according to the conversion rules...:
If NEWTYPE is a class, the class of the expression being converted must be NEWTYPE or must inherit from NEWTYPE."
The case we have here is:
NEWTYPE is Sub, OLDTYPE is Base.
Base doesn't inherit from Sub
nor is the Base class equal to Sub class
BUT Base and Sub classes' implementation is THE SAME.
For me it's not logical that the latter runtime check isn't enough to get error free program run (as Dan said) - but I guess there are things we just have to accept even though they aren't too logical
Why does compiler not "filter out" this case (base class to subclass cast) if we are going to get runtime exception anyway??
I hope I'm not too confusing :roll:
Thank You very much!!!
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The compiler would need to know that b is a Base and not a Sub. In some cases it can but in others it can't. The general rule is that unless the cast is impossible (casting a String to a StringBuffer for example) the compiler assumes that the programmer knows what he/she is doing.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
I agree. Here's the link:
subject: Run time casting of Objects
It's not a secret anymore!