[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 "<code>Component comp</code>":
- (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).]