- Read Tutorial
- Watch Guide Video
Out of all of the object oriented programming techniques that I teach, polymorphism seems to be one of the terms that new developers seem to be intimidated by the most.
Personally I blame Christopher Strachey, the computer scientist who coined the term in the 60’s. Sometimes computer scientists want to feel smart so we use complex words, even for simple concepts.
The easiest way to understand how polymorphism works is to actually break the word into pieces:
- Poly – is Greek for ‘many’
- Morphism – from the Greek for ‘shape’ or ‘form’
If you put those two things root words together you can translate the word to mean:
“Many shapes or forms”
As far as a straightforward definition goes for how polymorphism works for development purposes:
Polymorphism is the ability for a method to have different behavior for inherited classes.
How Polymorphism Works
As an example, let’s take this Invoice class, I’m using the Ruby programming language however the concept is pretty much the same across languages. Here we have a basic Invoice class that prints out some details about the invoice.
Now if our application needs the ability to print out shipping labels, it would make sense to create a class called ShippingLabel that inherits from Invoice. If the ShippingLabel has the requirement of having to print out a summary that contains 90% of the data that was in the Invoice class’ summary method, instead of duplicating the code we can use polymorphism to define a method with the same name and then simply add in any custom behavior that the ShippingLabel needs that the Invoice doesn’t have. The super keyword is simply telling the program to first look at the parent method and include whatever it returns before processing the rest of the method, if I didn’t include super our new class would completely override the parent class method, and sometimes you want to do that, which is why it’s optional to use super.
Another popular way to utilize polymorphism is when you have a parent class with a method that takes no arguments and the child class has a similar method that needs to take arguments.
In this example we have our Invoice and ShippingLabel classes, and this time the shipping label needs to take an argument to see who signed for the shipment. In cases like this you can use polymorphism to define the child class’ method to take arguments, even if the parent method doesn’t.
In development circles there are a number of terms that are used that are helpful to know. Monkey patching and function overloading are both coding techniques that are similar to how polymorphism works.
One other important concept to know is that the programming workflow that I walked through today is called ad hoc polymorphism. There are a few other types of polymorphism, namely Parametric and Subtyping, however when it comes to traditional Object Oriented Programming, when developers mention polymorphism they typically mean the process I explained here.
I hope that this helps you understand how polymorphism works and you’ll be able to use it in a future project.