1
votes

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.

2
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. 1.8e9-where's-my-share m.

2 Answers

2
votes

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.

0
votes

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