Re: Polymorphism vs. Overloading

Mike Chapman <Mike.Chapman@muc.de>
Mon, 31 Oct 1994 22:23:00 GMT

          From comp.compilers

Related articles
[9 earlier articles]
Re: Polymorphism vs. Overloading ryer@dsd.camb.inmet.com (1994-10-28)
Re: Polymorphism vs. Overloading mac@coos.dartmouth.edu (1994-10-25)
Re: Polymorphism vs. Overloading joe@sanskrit.ho.att.com (1994-10-31)
Re: Polymorphism vs. Overloading geld@cs.sun.ac.za (1994-10-31)
Re: Polymorphism vs. Overloading ram@cs.cmu.edu (Rob MacLachlan) (1994-10-25)
Re: Polymorphism vs. Overloading billk@cs.ukans.edu (1994-10-31)
Re: Polymorphism vs. Overloading Mike.Chapman@muc.de (Mike Chapman) (1994-10-31)
Re: Polymorphism vs. Overloading jsm@id.dth.dk (1994-10-31)
Re: Polymorphism vs. Overloading sofkam@rpi.edu (1994-10-31)
Re: Polymorphism vs. Overloading andand@csd.uu.se (1994-10-26)
Re: Polymorphism vs. Overloading dekker@dutiag.twi.tudelft.nl (1994-10-31)
Re: Polymorphism vs. Overloading danhicks@aol.com (1994-10-31)
Re: Polymorphism vs. Overloading odersky@ira.uka.de (Martin Odersky) (1994-10-31)
[15 later articles]
| List of all articles for this month |

Newsgroups: comp.compilers
From: Mike Chapman <Mike.Chapman@muc.de>
Keywords: polymorphism
Organization: Compilers Central
References: 94-10-144 94-10-154
Date: Mon, 31 Oct 1994 22:23:00 GMT

Gabriela O. de Vivo <gdevivo@conicit.ve> wrote:


>Last week I was invited to join a Thesis (MsC) presentation.
>At some point a question raised about the exact difference between
>Polymorphism and Overloading.


Joseph H Allen (jhallen@world.std.com) wrote:
: The difference is purely syntactical. Calls to overloaded functions look,
: well, like function calls. Calls to polymorphic functions require a dot or
: '->' somewhere. Really, that's the only difference. Artificial semantic
: restrictions placed by certain languages aside, you can always move the
: identifier or address-expression from the left of the dot into the
: parenthasis as the first argument to generate an equivelent overloaded
: function call.


No, I think this is wrong.


The big difference is that how the selection of the function is done.


The selection of an overloaded function occurs statically based upon
the declared types of the parameters.


The selection of a polymorphic function depends on the types of the
object (the parameter before the '.' or '->' in most languages)
at the time of invocation.


So if you have Shape objects of which Rectangles and Circles
are specific kinds of shapes and have say an overloaded
function display and a polymorphic function draw then


after


Shape x,y;
                Rectangle r;
                Circle c;


                x := r; -- Rectangles and circles are shapes
                y := c; -- so these assignments are type safe.


For the overloaded function display


              display(x) means Shape's display
              display(y) means Shape's display
              display(c) means Circles display
              display(r) means Rectangles display


Then for the polymorphic function draw


              x.draw means Rectangles draw
              y.draw means Circules draw
              r.draw means Rectangles draw
              c.draw means circles draw


All (?) OO languages support the polymorphic version.
Almost all OO languages support only one special argument
which controls the dispatching (the one before the '.' or
'->'). As far as I am aware Ada 9x allows dynamic dispatching
on more than one argument and does not use the '.' notation.


Overloading is just an excuse to avoid thinking of meaningful
names for some of your functions and has no real conceptual
significance. You could just have well have written
              display_circle(c)
              display_rectangle(r)
etc.


Smalltalk supports only the draw kind of call,
Ada 83, Algol 68 support only the display kind of call.
'C' has overloaded standard operators (eg '+' on int's
unsigned ints, double's etc).
Ada 83 has overloaded everything (and overloads on the
result type as well as the arguments which is a concept
which seems to be hard to explain to inexperienced
programmers).
--
Mike Chapman
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.