Polymorphism in Programming Language

Adding functionality to your code is one of the key essentials for every old and new developer. Every great programming development requires adding functionality forms or methods that allow the code to take different forms and adapt to every changing situation that may be demanded from such code. It is like building a doll with multiple faces.

Etymologically, polymorphism is derived from two Greek words namely "poly" meaning "many" and "morphe" meaning "forms" which in a sense can be regarded as a "thing with many forms or shapes".

Enough of the Greek grammar. In a lay sense, polymorphism is the ability of an object to take up new forms without losing its existing properties. It is a functional paradigm that cuts across many disciplines from science to programming.

Polymorphism in Programming

In Programming, Polymorphism is an object-oriented programming (OOP) concept that allows a specific routine to use variables of different types at different times. It is useful because it allows developers to build objects with the same functionality, meaning building functions with the same name, which tends to behave exactly in the same manner. To build a mature OOP your program must oblige to the standard of polymorphism.

Writing efficient programs not just programs, requires lots of logic and understanding of the core functionalities and principles to help such code take new forms when demanded and at the same time adapt to new situations when required. Understanding the concept makes you a unique programmer who's up to the task.

So to understand what polymorphism is all about, let's consider some real-life examples:

  • Consider a house door with a visitor notification system or simply a doorbell. It could be said that the bell is the property of the entrance door object. This doorbell could be rung anytime a visitor visits which directly gives an awareness that someone is behind the door waiting outside.

  • Now consider the same bell hung on a bicycle which directly makes it a bicycle bell. This bell could be considered as a property of the bicycle object. This bicycle bell could also be rung, which gives awareness to the pedestrians that a bicycle is close behind them.

Noting the above instances, it is a general belief that the intent of ringing a doorbell is completely different from the intent of ringing the bicycle bell. But they both perform the same likelihood function but in different situations or environments as required of them.

The above example can now be coded in Javascript as follows:

const door = {
    bell: function() {
        console.log("Ring! Someone is outside");
    }
}

const bicycle = {
    bell: function() {
        console.log("Ring! Make a way");
    }
}

The above Javascript code contains the door and bicycle objects. A bell method (i.e. function inside an object) is passed as the object property into the two objects but prints different content to the screen upon inspection or call.

Now I can access the bell( ) method on the door object using the following syntax:

door.bell(); // Output => "Ring! Someone is outside"

I can also access the bell( ) method on the bicycle object like this:

bicycle.bell(); // Output => "Ring! Make a way"

At this point, one can conclude that the same name of a method can have opposite intent or meaning, depending on the object it belongs.

To explain further and make this code truly polymorphic, I can pass a new function like this:

function bellRing(thing) {
    return thing.bell();
}

In the above code, I declare a bellRing function that accepts a thing parameter - which I believe to be either a door or bicycle object when called or invoked.

Now if I call the bellRing function and pass the object door as its single argument, the following output will be displayed:

bellRing(door); // Output => "Ring! Someone is outside"

However, if I invoke (i.e. call) the bellRing function and pass the bicycle object as its single argument, the following output will now be displayed:

bellRing(bicycle); // Output => "Ring! Make a way"

With the above examples we now see that the same function produces different results based on the context in which it has been used.

Therefore, Polymorphism allows you to write efficient code that can change and adapt to any changing situation that has been demanded from it.

Polymorphism in Javascript Classes

To wrap it up, let's consider another example of polymorphism using Javascript classes:

class sourceOfWater {
    water() {
        console.log("The source is");
    }
}

class Ocean extends sourceOfWater {
    water() {
        super.water() // Calling super class sourceOfWater
        console.log("from the ocean");
    }
}

class Rain extends sourceOfWater {
    water() {
        super.water() // Calling super class sourceOfWater
        console.log("from rainfall");
    }
}

// Passing into a variable
var oceanWater = new Ocean();
var rainWater = new Rain();

In the above code, there's a parent class named sourceOfWater with the water( ) method. The Ocean class extends the sourceOfWater superclass and inherits some of its properties. The Rain class also extends the sourceOfWater superclass, with both extended classes having the water( ) method as their property. The subclass Ocean and Rain is now passed into the variable oceanWater and rainWater respectively using the keyword new.

I can now invoke and print the code as follows:

oceanWater.water() 
// Output
/*
The source is
from the ocean
*/ 

rainWater.water()
// Output 
/*
The source is
from rainfall
*/

In conclusion, polymorphism will help in making your code more efficient and versatile during production and help in reducing brain raking on getting the right variable names for your code. Remember to implement polymorphism in your next project.

Thanks for your time. See you in my next post.