A reader sent me an interesting question the other day. They asked if polymorphism and overloading were essentially the same thing.
My initial reaction was Huh?
What are people being taught if they think that these two concepts are the same thing?
But a quick Google search revealed that yes, many, many people are struggling to differentiate between these two terms – and the internet is full of conflicting and unhelpful information.
Not only that, but I found this statement (copied and pasted all over the web) from a book called “Object Oriented Programming in C++”:
Overloading is a kind of polymorphism; it is also an important feature of C++.
Now, I’m sure this is a very good book (I haven’t read it, so I can’t comment. Maybe I will read it. Then I can.) but this statement is BOUND to cause unnecessary confusion for C++ learners.
There is no explanation of why overloading is a kind of polymorphism, or how indeed polymorphism relates to overloading. It’s the kind of throwaway statement that I really hate to see in textbooks (in which I believe that everything should be clearly stated and clearly explained with zero ambiguity).
So what’s the answer?
The basic answer is no, they are NOT the same – but they are related.
Polymorphism in C++ describes the ability of different objects to be accessed by a common interface.
I’ve written about polymorphism before here.
Whereas overloading in C++ allows the compiler to determine which version of a method is to be called based on the parameters that are passed to it.
I’ve written about overloading (versus overriding) before here.
So, just to be clear:
Polymorphism relates to objects.
Overloading relates to methods.
Polymorphism is how we subclass different types of objects (e.g. drinks: tea, coffee, champagne).
Overloading is how we accommodate variations in instructions (e.g. serve tea (the object) on its own, serve tea with a biscuit, serve tea with cake).
But…
There is more to the story.
Because, academically, overloading is categorised as polymorphism.
WTF?
I know, I know, I just told you they aren’t the same, and now I’m confusing you.
Ignore, for a minute, what polymorphism means in C++ or Java, and think of it for now, as a higher level idea.
Poly = many
Morph = form
It’s an umbrella term for using the same interface for different behaviours.
The polymorphism that you know in C++ is actually, more accurately termed inclusion or subtype polymorphism.
In fact, I drew a nice diagram to save having to bandy all these terms around, because sometimes things are just much easier to explain visually:
So, in a nutshell:
- Polymorphism and overloading (in C++) are two separate concepts. They are not the same.
- Polymorphism in C++ is more specifically termed “inclusion polymorphism” or “subtype polymorphism”.
- Overloading in C++ is a type of polymorphism, called ad hoc polymorphism.
So, going back to the quote in the book – it is entirely correct, but causes confusion because the author does not distinguish between polymorphism as a general concept, and polymorphism as used to create sub classes in C++.
And going back to the original question, polymorphism and overloading are not the same, but they both utilise a polymorphic approach.