
Signature
I start many things but I have yet to finish a single one.
[Agree] [Disagree]
<http://www.slowlydownward.com/NODATA/data_enter2.html>
> This doesn't work for a good reason. The virtual machine always knows
> the real class of an object, no matter what you cast it to. And if you
> call a method on an object, the VM always calls the method of that name
> in the class that your object is a type of (and if it isn't there, it
> searches up the hierarchy). This is a part of polymorphism, one of the
> core concepts of object orientated programming.
This isn't really a good reason, and it certainly has nothing to do with
the core concept of polymorphism. The equivalent code would have worked
in C++ because the cast would have adjusted the object address, which
would have adjusted the effective vtable used. In Java the cast doesn't
do that, but it does do a real typecheck.
Just accept it as a difference between the two languages.
Jochen Schulz - 18 Jul 2006 13:52 GMT
* EJP:
>> This doesn't work for a good reason. The virtual machine always knows
>> the real class of an object, no matter what you cast it to. And if you
[quoted text clipped - 5 lines]
> This isn't really a good reason, and it certainly has nothing to do with
> the core concept of polymorphism.
Oh, really? (I ask because I am interested, not because I think I am
right and you are wrong).
I thought a classic example of polymorphism goes like this:
Say you have a base class (or an interface) <Widget> and a few derived
classes <Button>, <ScrollBar> etc. A method paint() is part of the
<Widget> interface which all descendants of <Widget> have to implement.
That way a <Window> object may tell all <Widget> instances it knows
about to draw themselves by calling their paint() method (and maybe
passing itself as a parameter).
As fas as I can tell, this is polymorphism which wouldn't be possible
when "up"casting an object to one of its baseclasses would later on make
method lookups start from that baseclass.
Or is my assumption wrong that passing an object to a method which takes
an instance of a baseclass of this object's class as a parameter is
equivalent to upcasting?
> The equivalent code would have worked in C++ because the cast would
> have adjusted the object address, which would have adjusted the
> effective vtable used. In Java the cast doesn't do that, but it does
> do a real typecheck.
Scary. (For someone who has never touched neither C nor C++.)
J.

Signature
I wish I could achieve a 'just stepped out of the salon' look more
often. Or at least once.
[Agree] [Disagree]
<http://www.slowlydownward.com/NODATA/data_enter2.html>
EJP - 19 Jul 2006 06:52 GMT
> The equivalent code would have worked
> in C++ because the cast would have adjusted the object address, which
> would have adjusted the effective vtable used.
Thinking about this overnight I've concluded that it is entirely
possible that I have forgotten everything I ever knew about C++ vtabs
ten years ago, so the above may well be complete BS.