Polymorphism vs. overloading

Ralph Johnson <johnson@cs.uiuc.edu>
Sun, 9 Sep 90 22:41:58 -0500

          From comp.compilers

Related articles
Polymorphism vs. overloading johnson@cs.uiuc.edu (Ralph Johnson) (1990-09-09)
Re: Polymorphism vs. overloading norman@a.cs.okstate.edu (1990-09-12)
Re: Polymorphism vs. overloading sakkinen@jyu.fi (1990-09-14)
Polymorphism vs. Overloading gdevivo@conicit.ve (1994-10-22)
Re: Polymorphism vs. Overloading jhallen@world.std.com (1994-10-22)
Polymorphism vs. Overloading nandu@cs.clemson.edu (1994-10-27)
Re: Polymorphism vs. Overloading norman@flaubert.bellcore.com (1994-10-24)
[30 later articles]
| List of all articles for this month |

Newsgroups: comp.compilers
From: Ralph Johnson <johnson@cs.uiuc.edu>
Keywords: polymorphism
Organization: Compilers Central
Date: Sun, 9 Sep 90 22:41:58 -0500

I don't like any of the definitions that I have seen so far.
Instead of just being rude, I'll give my own.

"Polymorphic" refers to a procedure definition. A polymorphic procedure
is one that can take arguments of many different types. Most people
seem to get this one right.

Overloading (in Ada and C++, at least) means that there are several
different procedure definitions that define procedures with the same
names, but with different argument types (or, in the case of Ada,
result types). The compiler figures out which one to call based on
the types of the arguments (or, in Ada, expected return type). This
has nothing to do with any kind of polymorphism.

Most of the answers seem to imply that the late-binding in
object-oriented programming is a form of overloading. This is
not true, since overloading means something that can be handled
by the compiler. In fact, late-binding can be considered to be
normal polymorphism, i.e. a procedure that can take arguments of
many different types.

Consider the C++ expression figure->display(). This can be thought
of as "select the right function for the type of figure and run it",
in which case one might think that it was like overloading, i.e.
there are several different procedure definitions and one of them
is being selected based on the type of one of the arguments. However,
it is more correct to think that it means to retrieve a field of
record figure, which is a pointer to a function, and execute the
function to which it points. In this case, the function being
executed can be of one of several types. Thus, the "select field"
operation is polymorphic (because it takes different types of
records and returns different types of functions) and the "evaluate
function" operation is polymorphic (because it takes functions of
many types and returns values whose type depends on the function),
but there is no overloading.

I like to call this distinguishing characteristic of object-oriented
languages "polymorphism implemented by late-binding of procedure
calls". It is not very short, but it is accurate. There are other
ways to achive polymorphism, but this is the kind that gives
object-oriented programming its power.

Ralph Johnson - University of Illinois at Urbana-Champaign

Post a followup to this message

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