## What is Polymorphism?

I was once asked in an interview, “Can you tell me what polymorphism is?”

A straightforward enough question. Or is it? This can be exactly the kind of question that catches you off guard. You’ve used objects for years and of course you know what polymorphism is. But can you explain it in a concise and intelligent manner? This 5 minute guide to polymorphism will lay it all out clearly, so next time someone asks you, you can say, “Yes, let me explain it to you.”

### The word

The word is greek in origin and translates literally to ‘many’ + ‘forms’.

### In programming

Polymorphism is a descriptive term used in sciences such as biology and chemistry, but it has also been adopted by the object oriented programming methodology.

In a single sentence, polymorphism describes the ability of different objects to be accessed by a common interface.

### What does that mean though?

At its most basic, an object is polymorphic if it is related to one or more other objects that can ALL be utilized using the same methods.

To illustrate this with a trivial (and very non-scientific) example, consider the following.

In a room stands a mouse, a cat and a dog. From the far corner of the room, a speaker emits the sudden sound of a balloon popping. The mouse, the cat and the dog all immediately turn toward the direction of the sound. The dog barks, the cat twitches its tail and the mouse sniffs the air.

Programmatically, we can summarize this bizarre scenario as follows:

The mouse, cat and dog are 3 unique objects. They are not the same, but they have some characteristics that are the same. Together, they descend from a more generic superclass, which we will call a mammal. Mammals have ears, which allow them to process sound. The sound of the balloon popping causes each ‘object’ to process the sound in its own way: barking, tail-twitching and sniffing.

These ‘objects’ are polymorphic. Why?

Because although they all behave in different ways, the same method can be used to interact with them, and that method derives from a superclass. And thus here is the crux of the whole thing:

You don’t need to know which type of object you are talking to in order to utilize the method to talk to it.

Polymorphism allows you to talk to an object, without knowing exactly what the object is. If an object is polymorphic, it could be one of many different forms defined by you, the programmer, and descended from a generic base class. All you need to know is what generic type you are talking to and the available methods to talk to it.

Once you’ve called upon the method, the polymorphic type of the object will determine exactly how it behaves in response.

So there you have it. Polymorphism in a nutshell ðŸ™‚

I’ve written up the example above in C++ code, so if you want to see it in action, with the actual nuts and bolts of declaring and using objects polymorphically, you can find that here.

1. Jesse Moody
Posted 19 February 2013 at 18:57 | Permalink

Very informational, I was surfing the web for a definition of polymorphism but no one (Not even my college textbook) explained the concept in a way that I could understand its usage in programming. Thanks

2. Faye
Posted 20 February 2013 at 08:11 | Permalink

Thanks. Polymorphism sometimes seems like an elusive and abstract concept so I really wanted to be able to get across exactly what it meant in a practical sense. Glad you found it useful!

3. Paul Kemner
Posted 3 November 2014 at 19:29 | Permalink

Nice quick summary!

A print command might be a computer example that make sense for some people.

If I say PRINT (filename), I’d like the print method to find out what type of file it is, and format it appropriately. It should recognize if my file is a .pdf, a spreadsheet, document, or web page and print a nicely-formatted page. I don’t want to remember different print commands for each file type.

If I said PRINT (filename,printername), that would be overloading. The print command would recognize that I gave it two parms, and that the 2nd is where I want the output to go. A third parm might tell it the number of copies.

4. Jim Kay
Posted 24 February 2015 at 18:27 | Permalink

Great explanation!
I’m heading into a job interview and I realized that even though I’m a programmer I was having a hard time thinking about how I’d explain polymorphism if I was asked the question in the interview.
Your answer is clear, concise and covers the issues that make polymorphism so valuable in software development.

THANK YOU THANK YOU THANK YOU!!!

5. Posted 28 February 2015 at 15:48 | Permalink

ðŸ˜€

6. Bhishan Bhandari
Posted 23 June 2015 at 16:32 | Permalink

Thank You, very nicely explained. Hope I utilize the explaination someday.

7. vasu sharma
Posted 9 July 2015 at 12:45 | Permalink

first of all thank you so much for this great explanation.

#include
#include
#define pi 3.14
using namespace std;
class detail
{
public:
int h,w;
public:
virtual int area()
{
}
int show()
{
cin>>h;
cin>>w;
cout<<"first no is"<<h<<endl;
cout<<"second no is"<<w<<endl;
}
};
class rectangle:public detail
{
public:
int are;
public:
virtual int area()
{
show();
are=(w*h);
cout<<"area is=="<<are<<endl;
}
};
class triangle:public detail
{
public:
int area1;
public:
virtual int area()
{
show();
area1=(h*w)/2;
cout<<"area is"<<area1<<endl;
}
};
class circle:public detail
{
public:
int area2;
public:
virtual int area()
{