• 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

What's the point in casting a superclass to subclass?

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have:
class A {}
class B extends A {}
B b = (B)new A();
You always get a ClassCastException. It isn't like you are allowed to call methods from class A or anything, right?
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brian Joseph:
If you have:
class A {}
class B extends A {}
B b = (B)new A();
You always get a ClassCastException. It isn't like you are allowed to call methods from class A or anything, right?


You're right, this would'nt be of any value. However, think about building your methods based on abstract classes or interfaces. You might have an abstract class (or interface) Shape and have the appropriate methods like area, center, rotate...
Now, let's say that in some method that deals with Shape as a parameter you want to do behavior that only exists on one of the subclasses. You have to downcast to the appropriate type, or you can't invoke the method. So, the Shape is not (only) a Shape, it is some subclass like Octagon. Assuming that Octagon has some methods that are not generally applicable to Shapes, you have to downcast: Octagon myOct = (Octagon) theShape;
Of course, you might test it first to avoid the ClassCastException: if (theShape instanceOf Octagon).
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally, when you perform a downcast, you're going to perform it on a variable reference of the parent class, not on an object, itself, like this:

In this case, I wanted to tell the pet to play dead but, supposedly, not all pets are able to do that. Playing dead is a behavior specific to a dog. Therefore, I needed to first tell the compiler that the variable p referenced a Dog, in particular. At that point, the compiler will let me invoke any methods that are accessible within class Dog.
I hope that helps,
Corey
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the example Corey mentioned, playDead() method will be called in class Dog regardless of the downcast, since the new object being created is an instance of class Dog.
Please correct me if I am wrong here.
Thanks,
Saket
 
Brian Joseph
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sakat, I believe you correct, but only if playDead() is part of the Pet class. Otherwise if you have a Pet reference to a dog object, you can't call dog-specific methods from it. You need a Dog reference to do that.
I do now see the practical example of downcasting. Thanks!
[ June 09, 2003: Message edited by: Brian Joseph ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Brian, that's right. Just to clear up any confusion, let me show some more complete code for the example.

In this code snippet, I've defined three classes, Pet, Dog, and Cat. Dog and Cat extends Pet.
In my test class, I first invoke the eat method on each Pet that I've created. This is fine because the Pet class has an eat method. Notice, however, that Cat also defines an eat method with the same signature as the one in the Pet class. This method is being overridden so line 2 invokes the method eat in class Cat while lines 1 and 3 invoke the eat method in class Pet.
Lines 4 and 5 both produce compiler errors because the class Pet does not have accessible methods named purr or playDead.
Lines 6 and 7 will both produce ClassCastExceptions because a Pet is not a Dog and a Cat is not a Dog. Line 8, however, works fine because p3 really references a Dog object and, now that we've completed the downcast, we can invoke the method playDead of that object.
I hope that helps,
Corey
 
Brian Joseph
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, it sure does. Thanks everyone. Actually, I just slapped myself in the head, because I've downcast many times, such as when pulling elements out of a Vector, I have to downcast to the type that I know is contained in there . I think I've been studying for the SCJP too much, I'm loosing my practical experience.
 
reply
    Bookmark Topic Watch Topic
  • New Topic