Signup/Sign In
LAST UPDATED: APRIL 17, 2023

Understanding Polymorphism with Real World examples

    In today’s world, everything that we imagined in the past is becoming a reality. Be it from 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 more accuracy. So, it is very crucial to understand programming concepts with real-life examples and context.

    What is polymorphism

    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 an 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 a different name to save one number for a contact, what you can do is 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).

    Method Overloading

    This was 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 contract and if 2 numbers are passed to the same method name, then two numbers will be saved under the contact.

    This is also known as Method Overloading.

    Method Overloading example OOPS


    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.

    Method Overriding

    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 code. Polymorphism is a very useful concept in object-oriented programming and it can be applied in real-world scenarios as well. After all, Real World and Polymorphism are best friends :)

    Conclusion

    polymorphism is a powerful concept in computer programming that allows objects or entities to exhibit different forms or states and exhibit different behaviors or functionalities based on their current form. In this article, we have explored the concept of polymorphism and provided real-world examples to help us understand its practical applications.

    I hope you have got an idea of what polymorphism actually is and how to understand It in different languages like Java, C++, and C#. We'll understand how to implement Polymorphism in programming languages in another tutorial!!

    Frequently Asked Questions on Polymorphism (FAQs)

    1. What is polymorphism in Java?

    As we have seen from these real-world examples, Polymorphism in Java is nothing different. It is a concept that means many forms. Any line of code can have different multiple meanings that depend upon many factors.

    2. What is polymorphism and types?

    Polymorphism is a concept in computer programming that refers to the ability of a single function or method to work with multiple types of data or objects.

    Polymorphism has two types -

    • Compile-time Polymorphism - Also known as static polymorphism, it occurs during the compilation phase of a program. It is achieved through method overloading, where multiple methods with the same name but different parameters are defined in a class or interface. The appropriate method is chosen based on the number or types of arguments passed during compile time.
    • Runtime Polymorphism - While this is known as dynamic polymorphism, it occurs during the runtime or execution phase of a program. It is achieved through method overriding, where a subclass provides its own implementation of a method that is already defined in its superclass. The method to be executed is determined during runtime based on the actual object type.

    3. What is static and runtime polymorphism?

    Static polymorphism and runtime polymorphism are two types of polymorphism in computer programming that differ in when the type of a function or method is determined-

    • Static Polymorphism: Static polymorphism, also known as compile-time polymorphism, occurs during the compilation phase of a program. It is achieved through method overloading, where multiple methods with the same name but different parameters are defined in a class or interface.
    • Runtime Polymorphism: Runtime polymorphism, also known as dynamic polymorphism, occurs during the runtime or execution phase of a program. It is achieved through method overriding, where a subclass provides its own implementation of a method that is already defined in its superclass. The method to be called is determined at runtime based on the actual object type that the method is invoked on.

    4. What is an example of a mobile phone polymorphism?

    A great example of polymorphism is a mobile phone. A mobile phone can act as a camera, alarm clock, telephone directory, and make calls, among many other functions. It can switch between these different functionalities based on the user's input or the current mode it is in.

    You may also like:

    -->A SELF MOTIVATOR. ALSO LIKES TO MOTIVATE OTHERS. -->ANDROID APP DEVELOPER AND LOVES TO CODE. -->HAVE A TYPING SPEED OF 55WPM. ALSO, FOLLOW ME AT ME https://www.quora.com/profile/Akshay-Chopra-23 TO READ MY INTERESTING ANSWERS ON VARIOUS TOPICS.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS