polymorphism: overloading vs. overriding

markus_kranz at gmx.net markus_kranz at gmx.net
Sun May 7 04:00:15 PDT 2006


In article <e3ki2e$2o7a$1 at digitaldaemon.com>, Mike Parker says...
>Markus Kranz wrote:
>> [The desired output]
>> 
>> Bar.doIt()
>> Bar.doIt(Bar)
>> 
>> The actual output
>> 
>> Bar.doIt()
>> Foo.doIt(Foo)
>> 
>> [snip]
>
>This line is the key:
>
>Foo foo = new Bar();
>
>foo is an instance of Foo, not an instance of Bar, so the appropriate 
>method (Foo.doIt(Foo)) is being called.

Sorry, but what I read is that foo is not just a Foo but even a Bar?!


>Java and C++ both have the same behavior.

I can confirm this at least for Java.
But someone told me D was not Java... ;-)


>What you are suggesting is unintuitive. Not all Foos are Bars, 
>but all Bars are Foos. If you want Bar.doIt to be called when you have a 
>an instance of Foo, you have to downcast.

To my knowledge in D as in Java all member functions are virtual.
That is why foo.doIt() actually calls the version in Bar - without any explicit
cast.
Why do you think the actual behaviour is intuitive while my proposal isn't?

I would have understood if

Foo.doIt()
Foo.doIt(Foo)

got your vote.

Regards
Markus





More information about the Digitalmars-d mailing list