File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting subclass to superclass type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting subclass to superclass type" Watch "Casting subclass to superclass type" New topic
Author

Casting subclass to superclass type

Sagarika nair
Ranch Hand

Joined: Aug 13, 2003
Posts: 39
This is just a code I was practicing on my own when I came across a problem I couldn't follow.

Line 2 and line 3 are fine.
What I am not able to understand is line 1 i.e c.disp()
When the subclass is cast to the superclass type now the reference variable c is of type car right?So why is it not calling the superclass disp() method but instead calling the subclass' disp() method?
I can understand the point of dynamic polymorphism when the code is as follows :
i.e Car c=new Civic();//b'cos the objest is of type Civic
but I am confused about how it works with casting Car c=(Car)(new Civic());
Can somebody please explain?
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy,
This line:
Car c=(Car)(new Civic());
doesn't mean anything, and isn't needed, since Civic already IS a subtype of Car.
So you can just pretend it isn't there. You only need to cast when the compiler cannot tell for certain whether the object is actually castable to the Reference type. So think of that code as being identical to:
Car c = new Civic();
Remember, polymorphism ALWAYS applies to the *real* object type, and NEVER to the reference type. The cast never affects the actual object type. That object is a Civic on the heap regardless of what you do. Casting never changes an object; it is simply a way to tell the compiler that, "Trust me, I know what I am doing, and this object over here CAN be referenced from this declared reference type..." (although it fails at runtime if you were LYING.
For example:
Dog extends Animal
Cat extends Animal
Animal a = new Cat();
Dog d = (Dog) a;
The code above would compile because you put the cast in, and because 'a' is-a Animal, and Dog is-a Animal, so it is possible (as far as the compiler is concerned) that in fact 'a' might really be a Dog (or a subtype of Dog). So this would be a runtime failure, not a compile-time failure. Why?
Because if somebody thinks they have a Dog, they are going to ask it to bark.
d.bark();
And of course, Cats don't bark
So, think of casting as something to tell the compiler that this object will pass the instance-of test at runtime. There is NOTHING you can do, ever, to change the type of the actual object. The only thing you are doing is changing the way someone *sees* and *treats* the object.
I can say:
Object o = new Dog();
Animal a = (Animal) o;
Dog d = (Dog) a;
All of these work, and there is still just ONE object, a Dog, on the heap.
BUT... there are now three different reference types REFERRING to that same Dog.
The Object reference, o, can call methods ONLY if they are part of class Object. So I can ask the Dog for his hashcode, but I can't ask him to bark, using 'o'.
With Animal, I can ask the Dog to 'eat' (assuming that is in the Animal class), but I can't ask him to bark. If I want true "Dogness" from that object, I have to treat it as the Dog that it is, and cast it to the Dog so that I can call bark(), a method defined ONLY in the Dog class.
And if I want the beFriendly() method, well, then I have to cast it to the Pet interface that Dog implements...
Pet p = (Pet) d;
p.beFriendly();
Reference type means: what you can ASK the object to do.
Object type means: what it really CAN do.
But with non-static methods, regardless of how you view the object (in other words, regardless of the reference type you're using to access the object using the dot operator), there is only ONE object on the heap, so you'll get that object's version of the overridden method. Always.
Good question, though!
cheers,
Kathy
Sagarika nair
Ranch Hand

Joined: Aug 13, 2003
Posts: 39
Thank you so much Kathy for the wonderful explanation.
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Originally posted by Sagarika nair:
Thank you so much Kathy for the wonderful explanation.

Her explanations are always wonderful especially when she talks about dog or when she talks to dog


Mani
Quaerendo Invenietis
 
 
subject: Casting subclass to superclass type