DEV Community

Cover image for OOP -Part 2 Polymorphism

OOP -Part 2 Polymorphism

sukrutpatil profile image Sukrut Patil ・3 min read

Hope you enjoyed reading my last blog. Today, we will talk about polymorphism. It is something that your Ex might have done a lot to you. What?? No… I am not talking about cheating. I am talking about the conversation where she will say something and that will have more than one meaning and context. Polymorphism is similar, here we define one object and that object is referenced to different classes due to which same object will have different meaning and context. But there is one difference between your Ex’s example and polymorphism concept. Unlike your Ex, in OOPs you can easily figure out the context of the object and the behavior of that object.
The bookish definition of polymorphism is: Object taking multiple forms is called polymorphism. There are two different concepts in polymorphism, overloading and overriding. But before this you need to know there some basic terminologies.
• Methods: When a behavior of anything is defined within a block and that block is placed inside a class than that is called method. It will look something like this. (Ignore the language, it’s not important at this point.)

In real world programming they are a lot bigger. But small or big a problem is a problem. I mean a method is a method. So, I was talking about the behavior, the behavior of above method is to return the sum of two numbers ‘a’ and ‘b’.
Method Overloading:
This is when I define two methods with same name but with different parameters. Look at the examples below, you will get a better idea:

Just simple as this. And when we create object of the class and call these methods, we just have to pass exact same number of parameters that a respective method has. And as all of this is done at the compile time it is called static polymorphism.

Consider methods as Poppins candy. They all are packed in same packet (we call it a class in OOPs). They all have different flavors (we call it behavior/ functionalities). Yet they all have same name. We don’t name them differently or describe them differently, isn’t it?

Method Overriding:
I hope you remember what I told you about inheritance. If not, read my previous article. Take example of super class ‘Human’. There are some methods defined in them as eyes, legs, brain, hands, etc. and their behavior within those methods. There are two subclasses Men and Women derived from superclass Human. But you know that the behavior of some methods is different in Men class than that of Women class. In such conditions we use the concept of method overriding. Here, we define the method with same name and parameters in subclass. Which means, we will also redefine method brain in ‘Women’ class and change its behavior to receive more hormones than neurons (If you know what I mean).
Method name will be same, parameters will also be same, only the behavior will be different.
That was all for today. Hope, you understood the concepts. And yes, You can correct me if I'm wrong (which I know I'm not).


Editor guide
thejscode profile image
Jaskirat Grewal

Great Examples. Well thought and organized post.

sumit profile image
Sumit Singh

My confusion has been resolved by just going through your article. Just keep putting this type of content for us... buddy

sukrutpatil profile image
Sukrut Patil Author

I sure will... Thanks for the support