Object oriented programming has a few core concepts. One of the most fundamental is Polymorphism. Here’s a really simple explanation:

  • Polymorphism is the ability to hijack an existing inherited method and add functionality to it.

If you’ve been reading this series on OOP principles (and you should have been!) then you’ll remember Inheritance. Here’s a quick recap:

***** START INHERITANCE RECAP *****

Let’s say you have a class to create a ‘Car’:

And now create a ‘FastCar’ class that inherits from ‘Car’:

Now FastCar has inherited all the properties / functions / whatevers from ‘Car’.

***** END RECAP *****

Polymorphism

So how does polymorphism fit in here? Well let’s say we wanted to Print details about the ‘FastCar’. Of course we can just call PrintDetails (as it’s inherited from ‘Car’). BUT….what if we wanted to change the PrintDetails function? That’s where polymorphism waltzes in the door to save the day.

Polymorphism caterpillar and butterfly
Polymorphism – Changing a method or function to do something extra that it couldn’t do before!

We can ‘override’ the original PrintDetails function, execute it as normal (using the ‘super’ keyword) and then add extra functionality!

So you’ll often see polymorphism associated with the word ‘override’. If you look at the class definition it will also inherit from another base class.

And the advantage of all this? Why, as if you need to ask….

Write less code. Avoid repetition 🙂

What is Polymorphism – Object Oriented Programming

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share This

Exhausted by your 9 - 5?

Would you prefer to work for half the time and earn double? The first step to your freedom is understanding how apps make money. Simply enter your email below and I will send you a free PDF describing the process.

If you're serious about improving your life then do not miss this opportunity.


You have subscribed! Please check your email to confirm this subscription and to receive your item!