File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Green Test #1 question #17 (Casting) 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 "Marcus Green Test #1 question #17 (Casting)" Watch "Marcus Green Test #1 question #17 (Casting)" New topic
Author

Marcus Green Test #1 question #17 (Casting)

kamilla miesak
Ranch Hand

Joined: Mar 06, 2001
Posts: 30
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

Joined: Jul 14, 2002
Posts: 86
the code will throw a ClassCastException.
the rules that you are referring to are Compile Time rules.
hope this helps,
Maria


SCJP 1.4
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: Jul 25, 2002
Posts: 25
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

Joined: Mar 06, 2001
Posts: 30
Thanks guys, it did make it clearer for me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marcus Green Test #1 question #17 (Casting)