Originally posted by Paul selby:
Also, to carry on from jason's example.
Truck t = new DodgeRam();
DodgeRam dr = (DodgeRam) t;
The explicid cast is needed here, since the compiler will go by the class of the object t's reference (which is Truck) and not by what the actual class of the object in t (which is a DodgeRam).
Originally posted by Pauline McNamara:
So an object of a class higher up in the hierarchy, a superclass, has to be explicitly cast down to a lower class..
Upcasting is done automatically and "sideways" casting is not allowed.
Originally posted by Paul selby:
Never mind me, Micheal. My contribution to the comments on casting was probably not really worthwhile. At the time I thought it was a good, relevant point to make, or to go with an old line "it seemed like a good idea at the time, your Honour".
Originally posted by Paul selby:
...
Okey-dokey. Now in the below example, an object is being placed into a reference variable of the same class, however its previous reference is of a different class.
Truck t2 = new DodgeRam();
DodgeRam d2 = (Truck) t2;
The compiler will go by the class of the object's reference. So in this class it will look at t2 as being of the Truck class not of the class of the actual object it contains (which is a DodgeRam).
Overriding goes by the class of the actual object and Casting goes by the class of the object's reference variable. At the time , it stuck me as interesting...
Originally posted by Paul selby:
DodgeRam d1 = new DodgeRam();
Truck t2 = (Truck) d1;
Overriding goes by the class of the actual object and Casting goes by the class of the object's reference variable. At the time , it stuck me as interesting, what can I say? Mea culpa.
Now to the point I was putting forth.
Obviously the example below is casting. The subclass is being placed into the reference/variable of the superclass. Different classes, so casting is needed.:
DodgeRam d1 = new DodgeRam();
Truck t2 = (Truck) d1;
Okey-dokey. Now in the below example, an object is being placed into a reference variable of the same class, however its previous reference is of a different class.
Truck t2 = new DodgeRam();
DodgeRam d2 = (Truck) t2;
Originally posted by Suma MM:
P.S: Pauline, I assure I will not digress this thread of yours by any further questions (just bear with this one, thanks!)
Originally posted by Paul selby:
This has been good.
Originally posted by Dirk Schreckmann:
Assuming appropriate access modifiers, a child can use the parent's toys either through inheritance or with super. A child cannot use the grandparent's toys through inheritance or super if the parent is hiding or overriding said toys. Said child could create an instance of the grandparent, and then use the grandparent's toys (heh heh - eat that parent).
Originally posted by Michael Matola:
This is similar to my bad code scenario above. If a grandchild thinks it needs to subvert its (immediate) parent and use its grandparent's version of a method that the parent overrode, then it's time to consider whether the parent's overriding was appropriate to begin with.
Originally posted by Michael Matola:
Right. A class can call super only on its own immediate superclass.
Think of it this way. All this business about calling superclass methods can be thought of as an implementation detail of a given class, which should be hidden from users. You as an external user of that class can only interact with that class or its instances through its public interface (constructors, methods, fields).
But is there something you're writing in which you think you need to do this sort of thing?[/QB]
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |