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

Marcus Green Test #1 question #17 (Casting)

 
kamilla miesak
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
i don't understand why for the following question the answer is "Runtime Exception". I would think that the code will run fine. From what I understand for casting between classes, one of the two classes must be a subclass of another and it doesn't matter which one.
Thanks,
Kamilla
What will happen if you attempt to compile and run the following code?
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;
}
}
1) Compile and run without error
2) Compile time Exception
3) Runtime Exception
 
Maria Garcia
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code will throw a ClassCastException.
the rules that you are referring to are Compile Time rules.
hope this helps,
Maria
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kamila:
The rules for casting are very simple. You can
up-cast (i.e. from sub to base, some times known
as polymorphism) without any problems
because sub class has least implemented full
interface of base class. The reverse is a problem.
When you try to down-cast (i.e. from base
to sub), there are issues. The sub-class might
have additional interface (e.g. new methods) that is not present in
base. Therefore, compiler requires that you do an
explicit cast in the case of down-cast. When you
are expliciting down-casting the base the class, you are telling compiler that you know what you are doing and at run time object that base is
referring will actually be equivalent to a sub
class object. In case that promis is not kept,
you get run time class cast exception.
In your code, you are explicitly casting therefore
you do not get compile time error. But at run time
the object base is pointing to is not eqivalent to an object of sub-class, and you get run-time
excetpion.
Hope this help.
Thanks
Barkat
[ August 23, 2002: Message edited by: Barkat Mardhani ]
 
Jonathas Carrijo
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi kamila,
What you gotta realize is that casting does not transform the object type, but only changes reference data. Your b variable has a reference to an object of type Base, and at your casting, you change this reference, leaving the real object b points to unharmed.
Then, the casted reference is assigned normally to the s variable. But now, as s holds a reference to a Sub object, but actually the object is Base, the exception is thrown.
I don't know if what I just wrote here is any right, so please, ranch hands and bartenders, confirm or correct this, cause I need to get it right too =]
Thanx, and sorry for my english mistakes (I'm brazilian)
 
kamilla miesak
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, it did make it clearer for me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic