• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Casting urgently! Marcus Green Exam1

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, the compiler only knows one class must be a subclass of another but as detail,it will not know.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic