Polymorphism in Java with Examples | Object Oriented Programming

Prayukti Jain
5 min readJul 13, 2023

--

Polymorphism is a concept based on Object Oriented Programming or OOP. Talking about OOP, as the name suggests, is a programming model that is based upon the concept of Objects, that stores data. The data is in the form of attributes and methods. The complete standard of OOP revolves around binding data and methods, to restricts its usage outside the object.

Polymorphism | Overloading | Java | Polymorphism | Object Oriented Programming
Polymorphism | Object Oriented Programming

Polymorphism in a nutshell

Polymorphism, being based on OOP, operated within a object only. Also, In material science, Polymorph means existence of a solid material in more than one form. So, marking down the three key points to identify and understand Polymorphism would be:

  1. Existence of a process in many form.
  2. Which form gets executed depends on input.
  3. All the multiple forms should reside in a single unit.

Polymorphism in real life

So, we are now aware of the technical definition of what essentially Polymorphism is. Let’s now look up how and where we can see and use Polymorphism in real life. Consider the below example for instance.

Polymorphism | Overloading | Java | Polymorphism | Object Oriented Programming
Polymorphism | Object Oriented Programming

There is an owner, named John, who has 3 workers Sam, Toy and Joy, working for John to get Milk, Vegetables and Groceries, respectively. Now, if John wants to get some milk, what would he need to do to get it? John will recall all the workers and figure out which one of them works to get him Milk. He will then think of Sam, get him and ask him to buy some milk.

Now, in this complete course of action, task for John is to remember all the workers and their respective task, which can be bit strenuous and confusing. Here, Polymorphism can come to the rescue for John to work and get stuff done. Read through the next section, to understand how.

Polymorphism vs Overloading

So, in above example, we can see that a purchasing is a process that is existing in many forms. But we can not refer it to as Polymorphism, reason being, all the purchasing does not reside in a single unit. Now, here to achieve Polymorphism, we need to think through in such a way that:

  1. process of purchasing exist in many forms.
  2. all of the purchasing should reside in a single unit.
  3. and, which purchase needs to be done, depends upon the input.

This can be done in a feasible way, where John keeps only Sam for his purchasing and overloads his task to buy Milk, Vegetables and Groceries. This way we are overloading Sam, to attain Polymorphism.

Hence, we are good to say that we pulled off Polymorphism using Overloading, that is, Overloading is a technique or procedure to attain Polymorphism. Read through the next section, on how we can do this with piece of code.

Achieving Polymorphism using Overloading

In above section, we acknowledged that Polymorphism can be achieved via Overloading. Now, our trail comes to how we can perform overloading in our code to bring off Polymorphism. It would be simple with unravelling the three key points we went through earlier and decipher it according to the OOPs standard.

  1. Existence of a process in many form — functional overloading.
  2. Which form gets executed depends on input — number of arguments will decide the function to be executed.
  3. All the multiple forms should reside in a single unit — all the functions would reside in a single object.

Example of Polymorphism using Overloading

Let us consider that we need function to perform addition operation but the count of numbers we need to add can either be 2 or 3 or 4, any. Now, in order to get this done, we will just effortlessly overload the add function with 2, 3 and 4 parameters. All these functions would reside inside a single Object and which method would get executed will depend upon the arguments. Take a look at below given code for reference:

class Calculator {

// Method with 2 integer parameters
static int Add(int a, int b)
{

// Returns addition of 2 integer numbers
return a + b;
}

// Method with same name but 3 integer parameters
static int Add(int a, int b, int c)
{

// Returns addition of 3 integer numbers
return a + b + c;
}
// Method with same name but 4 integer parameters
static int Add(int a, int b, int c, int d)
{

// Returns addition of 4 integer numbers
return a + b + c + d;
}
}

class Main {

// Java’s driver method
public static void main(String[] args)
{

System.out.println(Calculator.Add(2, 4));
System.out.println(Calculator.Add(2, 4, 6));
System.out.println(Calculator.Add(2, 4, 6, 8));
}
}

The output of the above written program would look like this:

6
12
20

So now when we are familiar with Polymorphism, Overloading and how we can incorporate this in our code, let us see what is happening with the operating system when we are overloading a method. Read through the next section to understand more.

Polymorphism on OS Level

As we know, that for every function, there exist a pointer variable that stores the address for the function, that is, the pointer variable points to the function. Coming to the name of the pointer variable, the method name would be it in other cases, but what would happen if we have multiple methods with same name.

Signature and Type/Name Mangling in Polymorphism

In case of overloading, where multiple methods with same name exists in a program, OS performs a procedure to convert the name to what will be used as the pointer variable name and the process is called as Type/Name Mangling.

Method name is combined with the parameter type to generate a unique signature for a method, within a program. This signature, in turn, would be the name of the pointer variable, that points to the function. For reference, see below code snippet.

class Calculator {

static int add(int a, int b) => addintint
{
//some code
}
static int add(int a, int b, int c) => addintintint
{
//some code
}
static int add(int a, int b, int c, int d) => addintintintint
{
//some code
}
}

In this example, addintint, addintintint, addintintintint would be the signature of functions and would be pointer variable name.

In case if 2 signatures within a program is same, the program will fail to compile.

It is because compiler would not be able to address the functions, creating redundancy and would fail to call each one individually. That is why, every signature should be unique within a program.

Conclusion

In this article, we understood about Polymorphism, along with its real life example and how we can identify and achieve Polymorphism. We also got know how effortlessly we can incorporate Polymorphism in our code structure and what happens behind the scene, on the OS level.

Liked it, have a look

Do look out for other articles to get the knowledge about various topics and feel free to drop a comment for doubts or suggestions.

What are the top 10 useful functions of Underscore.js?

What are Decorators in Python?

Multithreading in Java

Understanding Apache Derby using Java

TCP/IP Socket Programming in Java

How to send HTTP Requests using React JSX?

--

--

Prayukti Jain
Prayukti Jain

Written by Prayukti Jain

Software Engineer at Microsoft | ex - Walmart | Content Writer | Open to Learn and Help

No responses yet