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 type 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 "type casting" Watch "type casting" New topic

type casting

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
What should be kept in mind regarding the comple and run without error for the following classes and references:
class One extends Object implements Runnable
class Two extends One implements Observer
Given the references:
One aone = new One();
Two atwo = new Two();
can someone explain which will give compile and run time error and why?
1. Object obj = aone;
Runnable rn = obj;
2. Object obj = aone;
Runnable rn = (Runnable)obj;
3. Object obj = aone;
Observer ob = (Observer)aone;
4. Object obj = atwo;
Observer ob2 = obj;
Ranch Hand

Joined: May 08, 2000
Posts: 76
here I am listing the thumb rules for casting and conversion given by RHE.

For object Reference Conversion,

  • An Interface may only be converted to its super interface or to Object
  • A class type may be converted to its superclass type or its superinterface type.
  • An Array may be converted to an Object type , a Cloneable interface type or an array of Superclass type

  • For Object Reference Casting
    Let's say the casting is like this

    1. Compile time

    2. When both OldType and NewType are classes, one class must be a subclass of the other.
    3. When both OldType and NewType are arrays, it must be legal to cast an element of OldType to an element of NewType (Refer to the prev. rule).
    4. We can always cast between an interface and a non-final object.
    5. Run Time

    6. If the NewType is a class, the underlying object in ot must be of NewType or must have inherited from NewType
    7. If the NewType is an interface, the underlying object in ot, must implement NewType.

Based on these rules,
1. Runnable rn = obj;
will not compile since obj is a reference to Object and Object does not implement Runnable.

2. Runnable rn = (Runnable)obj;
will compile and run, since you can cast to any interface type, and the underlying object in obj implements Runnable.

3.Observer ob = (Observer)aone;
will have runtime error, since the class of aone, One does not implement Observer.

4.Observer ob2 = obj;
will have compile time error, since you cannot cast an Object reference to Observer, since Object does not implement Observer.

One more thing to be noted while casting is,

  • Compile time rules apply to the references
  • Runtime rules apply to the underlying objects.

  • Hope, I am right,
    If there's anything wrong, please point it!!
    Thanks and regards,

    [This message has been edited by Suresh (edited May 21, 2000).]
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
thanx for the reply...i got it!
wana test?? give a few examples if u can?
thanx alot
I agree. Here's the link:
subject: type casting
It's not a secret anymore!