• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class ApBase extends Objects implementsRunnable
class ApDerived extends ApBase implements Observer
//Following variables created from above classes
ApBase aBase = new ApBase();
ApDerived aDer = new ApDerived();
which of the following compile and run w/o error ?
A) Object obj = aBase;
Runnable rn obj;
B) Object obj = aBase;
Runnable rn = (Runnable)obj; ..........Answer
C) Object obj = aBase;
Observer ob = (Observer)aBase;
D) Object obj = aDer;
Observer ob2 = obj;
Why not C also ?.
Can somebody help me in understanding this casting ?
 
Thandapani Saravanan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you check, the variable aBase refers an object of type ApBase. Now, trying to cast this object to Observer results in ClassCastException, which is a RunTimeException.
Basically you can't cast an object to an unrelated object.
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since at any point in the code <CODE>aBase</CODE> could be denoting an object of class ApDerived (which implements Observer), it could be possible to cast <CODE>aBase</CODE> to Observer. But only at runtime will it be determined whether this is possible.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kindly help with Summary:
<pre>
Object obj = aDer;
Runnable rn = (Runnable)obj;//line1.....OK
Observer ob = (Observer)obj;//line2.....OK
ApBase ap = (ApBase)obj; //line3.....OK
Object obj = aBase;
Runnable rn = (Runnable)obj; //line1....OK
Observer ob = (Observer)obj; //line2....NOT OK
ApDerived apd = (ApDerived)obj; //line3....NOT OK
</pre>
 
Dpk
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Object obj = aDer;
2. Runnable rn = (Runnable)obj;//line1.....OK
3. Observer ob = (Observer)obj;//line2.....OK
4. ApBase ap = (ApBase)obj; //line3.....OK
5. Object obj = aBase;
6. Runnable rn = (Runnable)obj; //line1....OK
7. Observer ob = (Observer)obj; //line2....NOT OK
8. ApDerived apd = (ApDerived)obj; //line3....NOT OK
Object
|
|
ApBase .... Runnable
|
|
ApDerived ...... Observer + Runnable (being subclass of ApBase)
Symbols: "|" - extends "...." implements
Casting is all about looking "UP" in hierarcy.
Line 2,3,4 are having an instance of Apderived, looking for casting: Runnable,Observer, and ApBase, all up in hierarcy. Apderived instance is all these things, so castable.
Line 7,8 are having an instance of ApBase, looking for casting Observer, and ApDerived, all down in hierarcy. ApBase instance is not all these things. It inherits what's all is above or what all it is, not below it!
I think you wouldn't get any compile error. But surely runtime error.
Please correct me if i am wrong !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic