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).]