aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting doubt" Watch "Casting doubt" New topic
Author

Casting doubt

janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
In the code below a ClassCastException is thrown at line1.

class Light{/*.....*/}
class LightBulb extends Light{/*....*/}
class SpotLightBulb extends LightBulb{/*....*/}

public class WhoAmI{
public static void main(String[] args){

Light light1 = new LightBulb();
SpotLightBulb spotRef = (SpotLightBulb)light1 //line1

}
}
Why can't an object of type LightBulb which is supertype of SpotLightBulb can be cast into SpotLightBulb type?Its a narrowing conversion of classes and an explicit cast is used.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11411
    
  16

think of it this way... if the cast were allowed, what happens when you call a method on spotRef that is defined in the SpotLight class? Since the actual created object was a LightBulb, it doesn't know anything about those methods... it can't.

or in another way, a LightBulb IS NOT a SpotLight, so you can't do that cast.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
Ok..i got it.But in what cases does the narrowing reference conversions apply?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11411
    
  16

I'm not sure i understand what you mean by narrowing reference conversions... but i THINK you mean something like this:

if you had

Light light1 = new SpotLightBulb();

now you have a variable that has a reference of type Light, but the ACTUAL object is a SpolLightBulb(); you CAN cast light1 to a LightBulb or a SpotLightBulb, since the actual object IS both of those.

if you don't cast it, you can only call the methods defined in Light (although the overridden methods in the subclasses would be the ones to run). if you cast it to one of the subclasses, you can then call those methods.
janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
This means that just being a super class doesn't make a class eligible to be explicitly cast to its sub class.

If i understood what you are saying this code should be right.

LightBulb light2 = new SpotLightBulb();//using light2 only methods in LightBulb class can be accessed
SpotLightBulb spotref2 = (SpotLightBulb)light2;//Now using spotref2 i can access both SpotLightBulb and LightBulb classes method.

Thanks a lot Fred, for your great explanation.
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
>> Light light1 = new LightBulb();
>> SpotLightBulb spotRef = (SpotLightBulb)light1

If that worked so would this and clearly this would never work:

Object object = new Object();
SpaceShuttleSimulator light = (SpaceShuttleSimulator)object;
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Casting doubt