aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting urgently! Marcus Green Exam1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting urgently! Marcus Green Exam1" Watch "Casting urgently! Marcus Green Exam1" New topic
Author

Casting urgently! Marcus Green Exam1

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi All,
Please help me clear the doubts.
What will happen if you attempt to compile and run the following code?
1) Compile and run without error
2) Compile time Exception
3) Runtime Exception

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;
}
}
I chose the 1, but the answer is 3. I am totally confused! because in RHE, it is ok to case the object reference at run time if one of classes must be a subclass of the other(it doesn't matter which one)(please refer to page118-119 in RHE if you have RHE). Am i missing something?
Mike
Kai Li
Greenhorn

Joined: Jun 26, 2000
Posts: 29
Originally posted by mikeliu:
Hi All,
Please help me clear the doubts.
What will happen if you attempt to compile and run the following code?
1) Compile and run without error
2) Compile time Exception
3) Runtime Exception

1.class Base {}
2.class Sub extends Base {}
3.class Sub2 extends Base {}
4.public class CEx{
5. public static void main(String argv[]){
6. Base b=new Base();
7. Sub s=(Sub) b;
8. }
9.}
I chose the 1, but the answer is 3. I am totally confused! because in RHE, it is ok to case the object reference at run time if one of classes must be a subclass of the other(it doesn't matter which one)(please refer to page118-119 in RHE if you have RHE). Am i missing something?
Mike

When I slove such questions,I first think about conversion then cast.
1. b is a class reference and it could only convert to its superclass.(RHE) Since it's illegal to convert to its sub class,
then it need cast.
2. As to cast,the compiler only check whether you have explict cast and it does not care who is base class and who is subclass.
(So if line 7 no (Sub) before b then it will generate compile error)

3. At the run time JVM will check the cast relationship between two classes.From RHE 118, since sub is a class, the class of the expression being converted must be sub or inherit from sub but base is super class, so it will generate run time exception.
Hope it will clear your doubt.
Kai,
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
when you do an explicit upward casting, the compiler assumes that you know what you are doing and does not check whether the class can really be cast. But you get a "ClassCastException" when you run it, because you can not fit a base class into a sub class. Subclasses can have more information than the base class. In this example, eventhough both classes have no members, it is illegal.
- Correct me if I am wrong.
----------------
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;
}
}
-------------------

Kai Li
Greenhorn

Joined: Jun 26, 2000
Posts: 29
yes, the compiler only knows one class must be a subclass of another but as detail,it will not know.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Kai,
My understanding now is the superclass cannot be casted to its subclass at run time. . But refer to RHE p118, the example shows line 6 "g1=(Grapefruit)c" is leagl at run time though the c is the superclass of g1. Could you please tell me why?
Mike
Kai Li
Greenhorn

Joined: Jun 26, 2000
Posts: 29
Not exactly, as to P118:
Line 5: c = g; //it means supclass already have the same type as g.
so line 6: g1 = (Grapefruit)c; //cast the same type as g1,it follows the runtime rules of RHE.
In summary, if you want to cast superclass to subclass, only the superclass reference already point to the subclass object,otherwise it will be illegal.
for example:assume Derived extends Base(both are classes)
Base b = new Derived();//b has the same type as the derived.
Derived d = (Derived)b; //it's ok
Kai
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting urgently! Marcus Green Exam1
 
Similar Threads
Casting from marcus greens Mock question
Marcus Green Test #1 question #17 (Casting)
casting
question on subclass and superclass
Please explain these answers