Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Run time casting of Objects

 
s khosa
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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;
}
}
Thanks
 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
mohd liyakhat ali
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
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!!!
Stasha
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic