Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C++

Polymorphism and Overloading in C++

Rate me:
Please Sign up or sign in to vote.
4.96/5 (11 votes)
2 Nov 2014CPOL3 min read 22.6K   11   14
Polymorphism and overloading in C++

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:

polymorphism

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.

Image 2 Image 3 Image 4 Image 5 Image 6 Image 7

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder FayeWilliams.com
United Kingdom United Kingdom
Programming shouldn't be a black art. Code is creative and beautiful. I write little programming guides for C, C++, GDB and Linux.

FayeWilliams.com

Comments and Discussions

 
Questionexcellent article, but... Pin
Vladimir E. Zyubin4-Nov-14 21:25
Vladimir E. Zyubin4-Nov-14 21:25 
AnswerOh! I have read the next statement Pin
Vladimir E. Zyubin5-Nov-14 17:26
Vladimir E. Zyubin5-Nov-14 17:26 
GeneralRe: Oh! I have read the next statement Pin
Faye Williams5-Nov-14 21:16
professionalFaye Williams5-Nov-14 21:16 
GeneralRe: Oh! I have read the next statement Pin
Vladimir E. Zyubin6-Nov-14 1:30
Vladimir E. Zyubin6-Nov-14 1:30 
GeneralRe: Oh! I have read the next statement Pin
Faye Williams6-Nov-14 2:01
professionalFaye Williams6-Nov-14 2:01 
GeneralRe: Oh! I have read the next statement Pin
Vladimir E. Zyubin6-Nov-14 2:50
Vladimir E. Zyubin6-Nov-14 2:50 
AnswerRe: excellent article, but... Pin
Faye Williams5-Nov-14 21:07
professionalFaye Williams5-Nov-14 21:07 
GeneralRe: excellent article, but... Pin
Vladimir E. Zyubin6-Nov-14 1:41
Vladimir E. Zyubin6-Nov-14 1:41 
QuestionKudos! Pin
Lucille Leitz3-Nov-14 9:49
professionalLucille Leitz3-Nov-14 9:49 
AnswerRe: Kudos! Pin
Faye Williams3-Nov-14 10:32
professionalFaye Williams3-Nov-14 10:32 
QuestionGood stuff! Pin
ITISAG3-Nov-14 7:47
ITISAG3-Nov-14 7:47 
www.cs.bu.edu/teaching/cpp/polymorphism/intro/

"...What is polymorphism?
In programmong languages, polymorphism means that some code or operations or objects behave differently in different contexts.
...In C++, that type of polymorphism is called overloading...."

yet another!

modified 3-Nov-14 13:57pm.

QuestionThanks! Pin
koothkeeper3-Nov-14 7:03
professionalkoothkeeper3-Nov-14 7:03 
AnswerRe: Thanks! Pin
Faye Williams3-Nov-14 10:33
professionalFaye Williams3-Nov-14 10:33 
QuestionNice explanation Pin
Nelek2-Nov-14 10:09
protectorNelek2-Nov-14 10:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.