Polymorphism is an OOP Superpower
By: Andrew Koenig-Bautista
Tags: Coding Interviews, Coding, Object Oriented, Codingbootcamp, Oop

Polymorphism is an OOP Superpower

Congratulations, if you’re reading this post you have made it through abstraction, encapsulation, and inheritance! That is no small feat. Last but certainly not least, today we will be covering polymorphism.

The Greek root of “poly” means “many” and “morph” means “form or change”. The simplest description of Polymorphism (as it relates to OOP) that I’ve come across is this: same name, different behavior. This description is, of course, a bit reductive, but it is a very helpful starting point nonetheless. Think about it like this: let’s say we have a woman named Sam. Sam is a mother, a wife, and a scientist. Though her name is always Sam, her behavior will be different depending on the context or the type of circumstance she finds herself in. With this in mind, you could say Sam is a polymorphic woman!

Newspaper: can function as a such, or as a hat! BAM! polymorphism. (Photo by jaikishan patel on Unsplash)

One function of polymorphism is that it allows inheritance to be implemented in a more dynamic way. If we had a parent class “Animal” with many different subclasses for each species, we might have those subclasses inherit a function named “sound( )”, and that series of events would be inheritance in action. However, we don’t want all of our animals making the same sound of course. It would be a bit awkward if elephants, lions, and cats all went “QUACK”. Polymorphism steps in and allows us to contextualize our inherited “sound( )” function.

With the addition of polymorphism, each class would be responsible for its own data. As such, dog.sound( ) would return “bark”, horse.sound( ) would return “neigh” and pig.sound( ) would return “oink”. The behavior of the function would be dependent on context. The function would have the same name in every subclass, but the behavior would be different. As mentioned above: same name, different behavior.

tiger.sound( ) returns “yawn” (Photo by Paula Borowska on Unsplash)

While it may not seem immediately obvious, the more time you spend reflecting on polymorphism (and all 4 pillars of OOP together) the more you will understand how enormously helpful they can be. Instead of creating a unique function for every single animal subclass, we have one single, universal function that behaves differently depending on the context.

Imagine the influence context might have for a communicate( ) function (Photo by Christian Mackie on Unsplash)

You might ask, “Well how is having just one polymorphic ‘sound’ function any different than having a bunch of different sound functions (aka dogSound( ), catSound( ), belugaWhaleSound( ), etc)? Either way, you would still have to take the time to create unique functionality for every animal subclass”. The difference is that with inherited, polymorphic functionality, once we know information about the parent class we will know information for any and every child class that has inherited from that parent class.

Imagine that we have thousands of child class creatures inheriting from the parent class “Animal”. If we see that the parent class has a sound( ) function, we now know that every single child class must have a sound( ) function as well. We don’t have to wonder what the function for a lion sound is called or if it has a different name than the function for a cow sound. Pairing inheritance and polymorphism takes the guesswork out of the equation.

Same name (camera) but unique functionality (Photo by Christian Mackie on Unsplash)

Polymorphism is a complex concept and I’ve linked several different sources at the end of this article that take deep dives into the intricacies of this subject. What I want you to take away from this high-level overview is that polymorphism is the process of creating code that behaves in accordance with the context it is in. That could mean our sound( ) function behaving differently for each animal, or it could also be a combine( ) function that behaves differently depending on the type of parameters it is passed (two integers vs two strings for example (this is called function overriding).

Some stack overflow examples

Polymorphism in C++

A simple, concise breakdown of different types of Polymorphism here

‘On Understanding Types, Data Abstraction, and Polymorphism’ -Luca Cardelli