For some reason, every time I have to go through an interview, I always forget what polymorphism is.
I have to study it over again, and to be honest, I feel kind of dumb every time because I have a hard time remembering what it is.
However, I finally discovered why I never fully understood it or remembered it, and it’s for three main reasons:
- Even though it’s simple, everyone defines it differently.
- There are two types of polymorphism that most of us aren’t aware of: static and dynamic (dynamic is the most common, which I’m covering in this post).
- Most of the definitions are written text and lack memorable and relatable examples.
In this post, I have made it my goal to finally explain polymorphism in a way that you won’t forget.
I will focus on dynamic polymorphism, which is what you will use and be asked about 99% of the time, break down the word to establish a foundation, show a technical standardized definition that you can rely on, and wrap it up with a unique, memorable, and relatable example you won’t forget.
Why won’t you forget this explanation?
Research in cognitive science shows that our brain remembers best when we link knowledge to vivid images, stories, and unique experiences.
It’s not about memorizing facts—it’s about creating memorable mental pictures that make complex ideas stick.
That’s exactly what we’ll do with polymorphism today.
I will link it to vivid images and a short story to make it fun, memorable, and relatable for you.
Breaking down the word Polymorphism
Firstly, to establish a foundation, let’s break down the word to understand its meaning.
Polymorphism is a word composed of two Greek words: “poly” and “morph.”
- “Poly” means “many.”
- “Morph” means “forms” or “shapes.”
So, polymorphism essentially means “many shapes” or “many forms,” right?
Well… almost.
The final part, “-ism,” refers to the practice or behavior. In other words, poly-morph-ism is “the behavior of taking many forms or shapes.”
Don’t worry about memorizing this right now; the example at the end will solidify this definition.
Technical definition
Next, let me provide you with a standardized technical definition you can rely on from now on:
Polymorphism in programming is a concept that allows objects of different classes to be treated as instances of their common base class, enabling them to be used interchangeably through method overriding.
What does this have to do with “the behavior of taking many forms or shapes”?
Think of the base class as the “default state” or the “common form” that provides a general blueprint for behavior. The different objects derived from this base class are like the “many forms or shapes” that this blueprint can take.
Don’t worry if it’s not very clear yet. For now, just remember that the keywords objects, base class, and overriding are three requirements to achieve polymorphism.
A memorable example you won’t forget
Alright, we’ve broken down the word and defined it—great! But what’s a memorable example that will really stick with you?
Think about something you experience every day: emotions!
Have you felt them? Of course you have—unless you’re a robot!
We constantly experience poly (many) emotions, and these emotions morph (change) from one to another based on different situations.
Remember when I mentioned that a “base class” is essential for achieving polymorphism in programming? The same idea applies to our emotions. To achieve polymorphism, there needs to be a base or default state. Let’s call this the “neutral emotion.”
But wait—how do we visualize our emotions changing and morphing?
Emojis!
Polymorphism explained with emojis
Let’s call this emoji “the base emoji”: 😐 (neutral emotion).
And to make it even more memorable, let’s illustrate it with a short story.
This is you! This is how you wake up every day—just meh, or neutral.
We have discovered the base class:
class BaseEmoji:
showEmotion() -> 😐
But this is not polymorphism yet. Let’s continue with the story.
Right after you wake up, you remember the message you sent to your crush last night.
You obviously sent a super creative message: “Hi” (genius!).
You quickly check your phone and see that she hasn’t answered yet.
Your hands start sweating, and you begin wondering, “Did I say something wrong?”, “Should I have said something different?”, “Omg, what should I do?”—you get the point.
What are you experiencing now?
Anxiety! 😰
Anxiety is a subclass of the base class, and let’s represent it like this:
class AnxiousEmoji extends BaseEmoji:
showEmotion() -> 😰
Now, this is polymorphism. AnxiousEmoji
can also display an emotion, but each class shows a different emoji based on its specific emotion.
Let’s continue.
After a little while, you hear a “ping!”
You’ve received a message!
Ahh… you earned it! Your aura is irresistible!
What do you experience now, Casanova?
Let’s say your internal code looks like this now:
class ExcitedEmoji extends BaseEmoji:
showEmotion() -> 😀 // Excitement!
Got the point now?
Remember that polymorphism is “the behavior of taking many forms or shapes.”
In this example, you saw how an emoji took the form of different emojis, thus demonstrating polymorphism.
The technical definition in action
Now, let’s break down the technical definition and see how each part is put into action:
Technical definition:
Polymorphism in programming is a concept that allows 1) objects of different classes to be treated as instances of their common 2) base class, enabling them to be 3) used interchangeably through 4) method overriding.
Code:
// 2) Base class
class BaseEmoji:
showEmotion() -> "😐"
// 4) Subclasses applying method overriding
class AnxiousEmoji extends BaseEmoji:
showEmotion() -> "😰"
class ExcitedEmoji extends BaseEmoji:
showEmotion() -> "😀"
// Function demonstrating polymorphism
displayEmotion(emoji: BaseEmoji):
print(emoji.showEmotion())
// 1) Objects of different classes
neutral = Emoji()
anxious = AnxiousEmoji()
excited = ExcitedEmoji()
// 3) Using the objects interchangeably
displayEmotion(neutral); // Output: 😐
displayEmotion(anxious); // Output: 😰
displayEmotion(excited); // Output: 😀
It’s that simple:
- The base class is defined.
- Subclasses are created to inherit its behavior.
- Method overriding is used to implement different behaviors.
- The objects can be used interchangeably throughout the code.
Conclusion
Polymorphism can be a challenging concept to understand, but when linked to a memorable example, it becomes easier.
I hope this example is memorable for you so that you don’t feel intimidated by the word “Polymorphism” ever again.
Remember that Polymorphism is just one building block in the OOP world, and its goal is to craft more dynamic and reusable code.
Was this example memorable for you? Can you think of other examples of Polymorphism that are more memorable for you?