Polymorphism (Overloading & Overriding)

Polymorphism’ Means A Single Line Of Code Represents Multiple Things. Which Can Be Used Based Upon The Situation. In Day To Day Life, Technology Is Evolving So Fast. So many things we imagined yesterday are getting reality today. Computers and Innovation have made numerous things a piece of cake! So, it stands essential to learn basic and key elements of programming which enable us to blaze a trail.

Polymorphism

Word ‘Poly’ stands for ‘Many’ and ‘Morphe’ stands for ‘Forms’ which is derived from the Greek language. ‘Polymorphism’ means ‘Many Forms’. Sounds as clear as mud? Well. It’s simple. ‘

Still head-scratching? Let’s understand by an example.
  1. Suppose, you’re also a pizza ? lover like me. You want to order 3 different pizzas namely, Pepperoni, Margherita and Peppy Paneer. Each pizza will have different toppings in it. Instead of writing a program for each pizza flavour, We create a class for each parent recipe and send one message to all, asking them to make a delicious pizza ?!
  1. Another classic example, Suppose a group of friends! You might have some friends whom you call by some weird names ?, and of course, there will be some friends whom you address using their real names only. Now if you want to save their contact numbers to your phone, you can write both, your friend’s real name and nickname. The person remains the same but has two names.

Now let’s talk in technical terms.

Polymorphism in Object-oriented programming

Polymorphism is a crucial part of Object-oriented Programming. It allows us to perform a single task using various ways. Technically, it allows us to have multiple implementations defining a single interface.

In polymorphism, objects act differently based on their situation. It is widely used to promote code reusability.

It includes two key elements 1. Overriding   and 2. Overloading

1. Overriding

The first example I gave was an example of OverRiding.

“If the subclass contains the same method as declared in the superclass, it is known as method overriding”

Specifically, If a subclass provides the specific implementation of the method that has been declared by one of its superclasses, it is known as method overriding.

It provides an IS-A relationship between these classes.

Let’s understand again using the above given example.

  • Pepperoni IS A Pizza
  • Margherita IS A Pizza
  • Pappy Paneer IS A Pizza

Output:

2. Overloading

The second example I gave, was an example of Overloading.

“Method Overloading is a programming concept that allows a class to have more than one method having the identical names, with a different number of arguments.”

In other words, multiple methods can have the same name with different parameters.

Let’s understand again using the above given example.

As said, assume you have two friends, out of which you address one with his real name whereas you address another one with both real name and nickname depending upon the situation. Now to save their contact number, you will require two following methods,

Output:

References:

1. “Method Overriding in Java” JavaTPoint Available: (https://www.javatpoint.com/method-overriding-in-java)

2.  “Method Overloading in Java” JavaTPoint Available: (https://www.javatpoint.com/method-overloading-in-java)

3. “Polymorphism in Java” GeeksForGeeks Available: (https://www.geeksforgeeks.org/polymorphism-in-java/)

Written By: Harsh Nagoriya

Reviewed By: Soutik Maity

Leave a Comment

Your email address will not be published. Required fields are marked *