1

Reading stackoverflow questions, the general consensus seems to be that overloading is not part of polymorphism.

However, my OOP lecture notes state that:

"There are four kinds of polymorphism: Parametric, Inclusion, Coercion, and Overloading".

In the notes, it refers to overloading with methods with different parameters, and also overloading operators, e.g. + in the sense of ints and floats.

Wikipedia also states "Ad hoc polymorphism is supported in many languages using function overloading."

Thus i'm confused as to why people say this isn't part of polymorphism, as it seems to be in my opinion; we have different forms for one method.

Could anyone elaborate?

Thanks.

user2864740
  • 60,010
  • 15
  • 145
  • 220
Brugsen
  • 603
  • 1
  • 6
  • 12
  • There is no central authority that sets up terminology for everyone. Some people consider only parametric polymorphism to be real polymorphism, others think ad-hoc and inclusion are OK too, still others add coercion. There is no one true classification. – n. m. could be an AI May 31 '14 at 15:08
  • possible duplicate of [Polymorphism vs Overriding vs Overloading](http://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading) – johnny Jun 02 '14 at 01:25

2 Answers2

2

If you take a strict definition of what the word Poly-Morphism means, then yes, overloading is polymorphism. The methods have the same name, different signatures and the runtime knows which method to use based upon the signature you use. That's many forms of the same method. It is not "classic" descriptions of polymorphism with classes and inheritance, animals, dogs, and cats, etc. Some languages have operator overloading. Is that many forms of the same type?

It really depends on what you say is polymorphing. If you say that many forms only relates to objects, they yeah, you can't have overloading as "real" polymorphism in the OOP sense because they are methods, not objects.

This can help, Polymorphism vs Overriding vs Overloading

You can see there are many opinions.

Community
  • 1
  • 1
johnny
  • 19,272
  • 52
  • 157
  • 259
  • thanks for the answer. Will read that in a moment but I'm busy right now. So I guess it's up to interpretation whether one wants to include overloading in polymorphism. On a side note, I know that coercion is part of it - which is kind of like implicit casting in a way. I was wondering if you think explicit casting would fit in to coercion, or if that really isn't polymorphism? Thanks. – Brugsen Jun 02 '14 at 01:34
  • Again, it depends on what you relegating to the terms of OOP. It is *object* oriented programming. I don't see it is polymorphism myself. See this for ideas too, http://stackoverflow.com/questions/8857763/what-is-the-difference-between-casting-and-coercing – johnny Jun 02 '14 at 01:38
0

Ad hoc polymorphism is considering the operators themselves to be like objects, which can be overloaded, yet still work in situations where the user is unaware of the specifics of the overload. This is basically the same as the motivation for polymorphism in objects, except with operators. http://en.wikipedia.org/wiki/Operator_overloading

etherous
  • 709
  • 4
  • 10
  • Operators are just functions with fancy syntax. Nothing special whatsoever about them. Overloading pertains to regularly-named functions and operators alike (some languages set special rules for operators for no good reason). – n. m. could be an AI May 31 '14 at 15:01
  • @n.m. That is correct. Languages which support operator overloading provided it as a form of syntactic sugar. In Java, for example, creating a method called 'add' would accomplish the same thing, but you wouldn't have the convenience of doing new Integer(5) + 42. In languages like that, ad-hoc polymorphism is forbidden. The rationale for this is normally that it can lead to sloppy, unreadable code – etherous May 31 '14 at 15:05
  • Please read it again. Java allows overloading of `add`, which is exactly the same kind of ad-hoc polymorphism as overloading of `+`. – n. m. could be an AI May 31 '14 at 15:13
  • @n.m. Except that 'add' is not an operator, and is therefore not operator overloading. While it is true that, in C++ for example, operator overloads are simply methods, this is not precisely the case conceptually – etherous May 31 '14 at 15:15
  • Please read it *again*. There is nothing special whatsoever about "operators". Fancy syntax is just that, fancy syntax. Spelling is not conceptual difference. – n. m. could be an AI May 31 '14 at 15:38
  • @n.m. We're arguing semantics, and not even important semantics. I concede – etherous May 31 '14 at 15:40