File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "casting" Watch "casting" New topic


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.

Tony Alicea

Joined: Jan 30, 2000
Posts: 3226
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
Ranch Hand

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

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
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:
subject: casting
It's not a secret anymore!