This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "casting" Watch "casting" New topic
Author

casting

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Oct 17, 1999
Posts: 117
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.


Saravanan
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
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.


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Feb 14, 2000
Posts: 10
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 !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting