8
votes

"Polymorphism is not the same as method overloading or method overriding. ... Neither ... are by themselves implementations of polymorphism".

This is a quote from wikipedia

However in the book "Object-Oriented Programming" Timothy Budd states there are "four different forms of polymorphism":

  • overloading (ad hoc polymorphism)

  • overriding (inclusion polymorphism)

  • polymorphic variable (assignment polymorphism)

  • generics

Who is right? Thanks

3
It depends on what you mean by "polymorphism". and see stackoverflow.com/questions/3343625/…unquiet mind
Wait... a different source contradicted Wikipedia? gaspFrustratedWithFormsDesigner
Don't look to comments for real answers... you will surely be disappointedShaded
It seems they define their terms differently, so as long as the context and definition of a conversation is known at the begining, either one of these could be correct. The problem is when Dev A takes the wikipedia defintion as more correct and Dev B takes Mr. Budd's definition as more correct, without communicating these decisions to each other.FrustratedWithFormsDesigner
@unquiet mind - thanks, I read the accepted answer in your link which states: "Polymorphism (very simply said) is a possibility to use a derived class where a base class is expected". Does this mean that in a dynamic language like Ruby, where there is no "expected" class, polymorphism does not apply?Question

3 Answers

3
votes

Polymorphism is a characteristic or feature of programming languages. Programming languages either support it or they don’t. Since programming languages fall under the umbrella of sometimes substantially different paradigms, different paradigms (functional programming or object oriented programming) may have slightly different interpretations and applications of HOW polymorphism is expressed in that particular paradigm.

As far as I know, in OOP polymorphism is considered one of the basic principles and a very distinctive one. Most of the object oriented languages have polymorphism among its many features. In a nutshell, polymorphism is best seen when the caller of an object with polymorphic implementation is not aware of the exact type the object is. Is is often a consequence of inheritance and casting, is also called subtype polymorphism, and works through the use of vTables.

I share the idea (along with many authors) that operator overload is a manifestation of polymorphism. So if you overload the == operator to take TypeA == TypeB, you are effectively interpreting TypeB as a TypeA if you are comparing elements in a list containing random elements of types A or B, you don't really care what comes in, since they can all be treated for equality. Like many other debates this one has defenders and haters.

But that's the end of the story for OOP.

In functional (declarative) languages (Lisp, F#) since the first class citizens are functions (vs Objects) polymorphism is expressed through relationships between functions and is manifested a bit differently. See Type Polymorphism

The last word I want to put out there is that I love Wikipedia as much as the rest, but you must always take articles with a grain of salt and never trust them blindly without confirming other sources. If you want to get the truth about the true principles of OOP, you should start here:

Object-Oriented Software Construction (Bertrand Meyer)

1
votes

I believe one of the best definitions I've seen about polymorphism refers to an object's type being discerned at Runtime. This seems to emphasize that the objects Runtime type may differ from it's declared type and that the methods invoked on the object will be matched during the Runtime process.

1
votes

I will leave the exact definitions to someone who knows better (from a complete purist point of view), but from a purely semantic point of view, those statements do not necessarily contradict each other.

One is listing 'four different forms' of something, and the other says that two of those four forms do not "by themselves" comprise the thing. One could argue that the 'four forms' writer isn't necessarily saying that each of those forms is, itself, a "complete" example of the whole, but that they are components of.


That said, I think that the 'four forms' writer is more correct, and the wikipedia writer is perhaps just trying to parse something a bit much. As are you. :p