• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reference conversion

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
<pre>
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
</pre>
regds
maha anna
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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).]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aha! Cool, my faith in humanity (or William Brogden at least) has been restored. Thanks William!
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic