• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Casting

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Do take a look at this code from Khalid Mughal's book (Pg. 247)
<code>
class Light {/* ... */}
class LightBulb extends Light {/* ... */}
class SpotLightBulb extends LightBulb {/* ... */}
class TubeLight extends Light {/* ... */}
class NeonLight extends TubeLight {/* ... */}
public class WhoAmI {
public static void main(String[] args){
boolean result1, result2, result3, result4, result5;
Light light1 = new LightBulb();
//String str = (String) light1;
//result1 = light1 instanceof String;
result2 = light1 instanceof TubeLight;
//TubeLight tubelight1 = (TubeLight) light1;

result3 = light1 instanceof SpotLightBulb;
//SpotLightBulb spotRef = (SpotLightBulb) light1;
light1 = new NeonLight();
if (light1 instanceof TubeLight) {
TubeLight tubelight2 = (TubeLight) light1;
}
}
}
</code>

In Page 178, the book says that "Casting the value of a superclass reference to a subclass type is called downcasting. After reading that I am at a loss to understand why the line
<code>//SpotLightBulb spotRef = (SpotLightBulb) light1;</code> would give a compiler error?

I would appreciate any help that would make me understand this.
Thanks in Advance
Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 08, 2001).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this is because of..."The Destination Type must be a superclass of the Source Type"?...If so, where does downcasting come into the picture?
Regards
Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 08, 2001).]
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I uncommented that line and javac compiled with no problem. I was using jdk1.3 on Windows98. Explicit casting between two classes that are in any kind of heirachical relationship is allowed. Implicit conversion only works up the inhertance heirachy.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cameron,
I was wrong in saying that it was a compiler error in my first post. In fact it is a ClassCastException.
I am using JDK 1.3 in Windows 2000...Here's the output that I got after uncommenting that line.
--------------
Exception in thread "main" java.lang.ClassCastException: LightBulb at WhoAmI.main(WhoAmI.java:19)
--------------
Waiting for your comments.
Shyam
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no such compile error for
but a run time.
The compiler only checks for a hierarquical relationship and for a cast operator if donwcasting. However at runtime any subclass object can be upcasted but only the ones that are really of the subclass type can be downcasted. As a ligthBulb is not a spotLigthBulb object the exception is thrown at run time.
The same can be said for

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jose,
I followed your comments and added this snippet to the existing code. It worked just fine.


So, if I understood you completely... Downcasting is only possible when the superclass reference points to a subclass object. And such being the case, the superclass reference can be downcast to a subclass type (an object of which the superclass reference is pointing to.)
Is this what you meant when you said "

However at runtime any subclass object can be upcasted but only the ones that are really of the subclass type can be downcasted."

?
Do tell me whether I have understood things right.
Thanks
Shyam

[This message has been edited by Shyamsundar Gururaj (edited September 08, 2001).]
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you got it
otherwise an object could receive messages(methods) for which is not prepared for (not corresponding to its real type)
 
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic