Author Topic:   Reference conversion
Linda Xu
greenhorn
posted March 07, 2000 12:12 PM         
In Bill Brogden's Exam Cram book, page 110. When talking about " a class reference can be converted to:", it said" any class reference with runtime check", what does it mean?

Does it mean I can use explicit cast to cast a class reference to any class reference without complaint in compile? But I tried the following code:
...
Frame f = new Frame();
BufferedReader bf = (BufferedReader) f;
..
When compile, there is error message say "Invalid cast from java.awt.Frame to java.io.BufferedReader".

So, Does that mean I can cast to some "Valid" class reference? What classes are valid? How to understand Bill's statement in book?

Thank you very much!

maha anna
bartender
posted March 07, 2000 12:31 PM             
You can cast a class to another class provided these 2 classes are in the same branch of the class hierarchy and either of these 2 classes MUST be superclass of the other. Otherwise the compiles easily finds the classes are totally unrelated and throws "Invalid cast error".

eg. class Super {}
class Sub extends Super{}
class SubSub extends Sub {}
// Super sup; Sub sub; SubSub subsub; String str;
// sup = sub; //ok
// sup = subsub; //ok
// subsub = (subsub)sup; //ok Since sup's type is superclass of subsub's type
// subsub =(subsub)str; //not ok Since these 2 object's type are TOTALLY unrelated

regds
maha anna

Jim Yingst
sheriff
posted March 07, 2000 12:46 PM             
[I see maha anna was too fast for me - still, I'll leave this up as it has some additional info. - Jim]

I think Bill's statement is not entirely correct, as you've discovered. (But I don't have the book, so maybe there's more to the statement?)

If you cast a reference to the same type as the reference type, or a superclass or superinterface, then the compiler will say OK, and won't even bother to check at run-time (the cast is guaranteed to be correct, and it's unnecessary).

If you cast a reference to a subclass of the reference type, or to any interface not already implemented by the reference type, then the compiler will say OK for now, but will tell the JVM to check at run-time, because the reference could refer to a subclass instance, which may also have implemented other interfaces.

If you cast a reference to an unrelated class (not interface), then the compiler knows there is no way the reference can ever hold an object of that type, and so the compiler will complain at compile time.

So, for a variable declared as "Component comp":

  • (Object) comp is OK, and the cast was unnecessary - no run-time check.
  • (Frame) comp is OK at compile time - comp might actually be a Frame (or Window, or Dialog...), so there will be a run-time check.
  • (Runnable) comp is OK too at compile time. Even if there are no existing Components which implement Runnable (I think), you could still create one yourself. So again, a run-time check.
  • (String) comp will fail at compile time. No Component can ever be a String.

[This message has been edited by Jim Yingst (edited March 07, 2000).]

William Brogden
unregistered
posted March 07, 2000 02:51 PM           
quote:
Originally posted by Linda Xu:
In Bill Brogden's Exam Cram book, page 110. When talking about " a class reference can be converted to:", it said" any class reference with runtime check", what does it mean?


Look at the top of the page. It says these are the conversions that can be legally coded. - In other words, the compiler will not throw them out. To pass the runtime check the actual reference will have to be of the proper type.

William Brogden
unregistered
posted March 07, 2000 03:14 PM           
quote:
Originally posted by Linda Xu:
In Bill Brogden's Exam Cram book, page 110. When talking about " a class reference can be converted to:", it said" any class reference with runtime check", what does it mean?


Whoops, that first reply was off the mark, now that I re-read the question, let me try again. The cast it is talking about is
a class reference, not a reference to an instance of a class. As in something like this:

Class c = Class.forName( java.awt.Frame );

Jim Yingst
sheriff
posted March 07, 2000 03:36 PM             
Aha! Cool, my faith in humanity (or William Brogden at least) has been restored. Thanks William!

|