Mortein Killer’s Polymorphism [POLYMORPHISM Simplified]

Hi all. The concept of polymorphism is simple once you catch it. Here is a simple analogy on polymorphism for you.

This is Sri, once again with another amazing analogy that will clear all the doubts in the concept of Polymorphism. Looking at the title, you might be thinking whether this is a crime story 🙂 However, it is in one way! But the killer in the title is Mortein Mosquito the world-famous Cockroach killer.

Many of us would have seen this ad “Black Hit for Mosquito and Red Hit for Cockroach“.

How many of you, did actually use the red Mortein for the cockroach and still wondered why the cockroach is still alive? Obviously, I am on your side. In those scenarios, we start pondering on why the hell do they have separate bottles, anyways? 🙁

Note: I did not get Red bottle with mosquito image so adjust with green 🙂

Mortien cockroach                mortein mosquito

Now coming to our topic polymorphism. This would be the case for the users of your code if no polymorphism exists.

(Note: You can also see my previous post about Abstraction in OOPS in this post “Ammi Kallum Abstraction um”)

Suppose, somebody, ask you to write a math library. Imagine it includes functions like addition, subtraction, etc. They will use the library for their calculator program.

calculator

The day comes, you delivered the code and along with given 100 pages document saying, Call addFloat for adding float numbers, addInt for adding integers, etc. Hey, dear programmer for just adding two numbers how many pages do I have to refer to find the correct function name. Because to add float, you have written function with name addFloat, for integers addInt.

man-confused-sad-hi

int addInt(int num1,num2);

float addFloat(float num1,float num2);

This is really cumbersome and irritating for the team and the project also appears too big. Even for small projects, this is very difficult for development and maintenance.

To overcome this issue, compile-time polymorphism came into the picture. Don’t name the functions differently. If the function is to add, just name the function as add again. The function for adding integers and float will have the same names. But for the compiler to identify it’s not a redefinition, we have to give some identification marks like-

1.Make the datatype of the function parameters different or

2.Make a difference in the number of parameters.

int add(int num1,num2);

float add(float num1,float num2);

So that compiler can identify this as a polymorph of add function. But you may be asking how a compiler is doing this magic.

kung-fu-panda-21

The secret sauce is Name Mangling, even though we name both the functions as add for adding float and adding integers. The compiler will internally change the name of the function so that it will become 2 different functions for the compiler.

NameMangled functions

int _Zpiadd(int num1,int num2);

float _KLpiadd(float num1,float num2);

These are just samples!

So whenever we call add, the compiler will look for the type of parameters and call the respective the NameMangled add function.

We can see Run Time Polymorphism in our next post.

I heard that hitting Like is good for health 🙂 So just hit the like button. And for more technical videos visit us GUVI.

Thanks,

Sri.

About Me:

With10 +yrs of experience in the software industry, I am very passionate about coding and love to teach others. I have considerably good experience in languages like C, C++, VC++, Java, PHP.

Now a full-time entrepreneur and taking care of GUVI.

You may also like...

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] Mortein Killer’s Polymorphism Mother of OOPS – Idly Kadai Aaya […]