"Polymorphism is not the same as overloading a method or overriding a method."

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

This is a quote from wikipedia.

However, in the book Object Oriented Programming, Timothy Budd claims that there are “four different forms of polymorphism”:

  • overload (special polymorphism)

  • redefinition (inclusion polymorphism)

  • polymorphic variable (destination polymorphism)

  • generics

Who is right? Thanks

+7
source share
3 answers

Polymorphism is a characteristic or feature of programming languages. Programming languages ​​either support it, or they are not used. Since programming languages ​​sometimes come under the concept of essentially different paradigms, different paradigms (functional programming or object-oriented programming) may have slightly different interpretations and applications of HOW polymorphism in this particular paradigm.

As far as I know, OOP polymorphism is considered one of the basic principles and very distinctive. Most object-oriented languages ​​have polymorphism among its many functions. In a nutshell, polymorphism is best seen when the caller with the polymorphic implementation does not know the exact type of object. It is often a consequence of inheritance and casting, also called a subtype of polymorphism, and works using virtual tables.

I share the idea (along with many authors) that operator overloading is a manifestation of polymorphism. Therefore, if you overload the == operator to accept TypeA == TypeB, you effectively interpret TypeB as TypeA, if you compare items in a list containing random items of types A or B, you don't care what happens, as you can access them for equality. Like many other debates, this one has advocates and haters.

But this is the end of the story for OOP.

In functional (declarative) languages ​​(Lisp, F #), since first-class citizens are functions (vs Objects), polymorphism is expressed through the relationship between functions and appears a little differently. See Type Polymorphism

The last word I want to express is that I love Wikipedia just like everything else, but you should always take articles with salt and never trust them blindly without confirming other sources. If you want to know the truth about the true principles of OOP, you should start here:

Object Oriented Software Development (Bertrand Meyer)

+2
source

I believe that one of the best definitions I've seen about polymorphism refers to the type of object that is recognized in Runtime. This seems to emphasize that the Runtime type of objects may differ from its declared type and that methods invoked on the object will be matched at run time.

+1
source

I will leave exact definitions to those who know better (from the full point of view of the purist), but from a purely semantic point of view, these statements are not necessarily contradictory.

One of them represents the "four different forms" of something, and the other says that two of these four forms do not "in themselves" constitute a thing. It can be argued that the writer of the “four forms” does not necessarily say that each of these forms in itself is a “complete” example of the whole, but that they are components.


However, I think that the author of the “four forms” is more right, and the Wikipedia writer may just be trying to make out a little. Like you .: P

+1
source

All Articles