Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!think.com!mintaka!spdcc!esegue!compilers-sender From: johnson@cs.uiuc.edu (Ralph Johnson) Newsgroups: comp.compilers Subject: Polymorphism vs. overloading Keywords: polymorphism Message-ID: <9009100341.AA17336@m.cs.uiuc.edu> Date: 10 Sep 90 03:41:58 GMT Sender: compilers-sender@esegue.segue.boston.ma.us Reply-To: Ralph Johnson Organization: Compilers Central Lines: 46 Approved: compilers@esegue.segue.boston.ma.us 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 -- Send compilers articles to compilers@esegue.segue.boston.ma.us {ima | spdcc | world}!esegue. Meta-mail to compilers-request@esegue.