Technology5 min read

Understanding Polymorphism with Real World examples

Abhishek Ahlawat

By Abhishek Ahlawat — Recently

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

Summarize this article using AI

In today’s world, everything that we imagined in the past is becoming a reality. Be it from the Sci-fi movies or our imagination and ideas, we have worked towards turning them into reality. Similarly, programs (computer and programming languages) were designed to solve problems faster and with accuracy. So, it is very crucial to understand programming concepts with real-life examples and context.

Today, we will learn How Polymorphism is connected to the real world? Below we have a conversation between the Real World and Polymorphism. Let’s have a look at the conversation.

Real World: Hye Poly! why you look so sad?

Polymorphism: Nothing dude, I am missing you a lot.

Real World: Thank you, buddy. Even I miss you a lot. But don’t you think we are always connected?

Polymorphism: Are we? Because I don't think we are.

Real World: Yes, we are always connected. We are always together and we will be best friends forever.

Yes, that was a very naive conversation, but there is a hidden fact in there.




What is Polymorphism?

In simple words, it means "Many Forms". What this means in the object-oriented programming language like Java is that, a single line of code (Here, a single line of code can refer to the name of a method, or a variable, etc.) can have multiple meanings, and which meaning is to be used depends upon various factors.




Now let’s see how Polymorphism and the real world are linked together.

  • Consider your Mobile phone. You can save your Contacts in it. Now suppose you want to save 2 numbers for one person. You can do it by saving the second number under the same name.

Similarly, in an object-oriented language like Java, suppose you want to save two numbers for one person. You must have a function, which will take the two numbers and the person name as arguments to some function void createContact(String name, int number1, int number2).

Now it’s not necessary that every person will have 2 numbers. Many other contacts might have only a single number. In such a situation, instead of creating another method with different name to save one number for a contact, what you can do is that you can have the same name of the method i.e. createContact() but instead of taking 2 numbers as parameters, you can take only 1 number as parameter in it i.e. void createContact(String name, int number1).

This is Polymorphism. There is only one method named createContact(), but it has two definitions. Now which definition is to be executed depends upon the number of parameters being passed. If 1 parameter is passed, then only a single number is saved under the contact and if 2 numbers are passed to the same method name, then two numbers will be saved under the contact.

Method Overloading example OOPS

This is also known as Method Overloading.




Let's see another real-world example of polymorphism.

  • Suppose you go to an Ice Cream Parlor (ABC Ice Cream) near your home one day and you buy a vanilla flavored ice-cream. A week later, while traveling to the town nearby, you spot another Ice Cream Parlor (of the same chain, the ABC Ice Cream). You went to that shop and found a new variant of the Vanilla flavor ice-cream which had a twist of Chocolate flavor too in it. You really liked the new flavor. Once back home, you again went to the ice cream parlor near your home to get that amazing new flavor ice cream, but unfortunately, you couldn't, because that was a specialty of the parlor which was located in the neighboring town.

Now relating this to the functioning of an object-oriented language like Java, suppose you have a class named XIceCream which includes a method named icecream(). Using this method, you can get a vanilla flavor ice cream. For the ice cream parlor in the neighboring town, there is another class YIceCream. Both the classes XIceCream and YIceCream extends the parent class ABCIceCream. The YIceCream class includes a method named icecream(), using which you can get a vanilla and chocolate flavor ice cream.

class ABCIceCream {
    public void icecream() {
        System.out.println("Default Vanilla Icecream");
    }
}

class XIceCream extends ABCIceCream {
    public void icecream() {
        System.out.println("Default Vanilla Icecream");
    }
}

class YIceCream extends ABCIceCream { 
    // This is overridden method
    public void icecream() {
        System.out.println("Vanilla with Chocolate Icecream");
    } 
}

So, instead of creating different methods for every flavor, we can have a single method icecream(), which can be defined as per the different child classes. Thus, the method named icecream() has two definitions- one with only vanilla flavor and one with both vanilla with chocolate flavor.

Which method gets invoked depends upon the type of object i.e. the object is of which class. If you create ABCIceCream class object, then there will be only one vanilla flavor in available. But if you create YIceCream class object, that extends ABCIceCream class, then you can have both vanilla as well as vanilla with chocolate flavor.

This is also known as Method Overriding.

Thus, Polymorphism makes the code more simple and readable. It also reduces the complexity of reading and saves many lines of codes. Polymorphism is a very useful concept in object-oriented programming and it can be applied in the real world scenarios as well. After all, Real World and Polymorphism are best friends :)


SHARE YOUR THOUGHTS WITH US!



Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops — put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next