Back To Basics : Polymorphism in Java

What is Polymorphism

Polymorphism is the idea of invoking  different methods having the same name, but different implementations.

The following scenario explains it best. I have class called MyMathClass. Now, i need a method which will add two integers. This is what i do.

Class MyMathClass {

public int add(int a,int b) {

return (a+b);



Job well done. You create an instance of MyMathClass and use it to invoke the add  method which returns you the sum of two inetegers passed to it. That’s neat. 🙂 🙂

But what do you do if you want to expose a method which adds 3 integers or for that matter n  integers passed to it. Imagine a situation where there is no concept called polymorphism, you will have to create different implementations of the same add method with different names. Assume that you need to package and expose this MyMathClass as an API. Now, the user’s of this API will need to know the names of the different methods which performs the same functionality – add. Now that does not look very neat. 😦 😦

This is where polymorphism makes its entry.  Polymorphism enables Class instances ( objects ) to refer to different method implementations having the same name. This is possible by varying the method signature ( the order or the number of parameters taken in as input by the method ). Find below, how MyMathClass gets transformed after polymorphism.

Class MyMathClass {

/* Method to add two integers */

public int add(int a,int b) {

return (a+b);


/* Method to add 3 integers */

public int add(int a,int b,int c) {

return (a+b+c);


/*Method to add n integers */

public int add(int a[],int n) {

int sum=0;

for(int i=0;i<n;i++){

sum = sum+a[i];




public MyMathClassDemo {

public static void main() {

MyMathClass myMathClassObject = new MyMathClass();

int sum = myMathClassObject.add(2,3); // Calls the add method for adding the two integers

sum = myMathClassObject.add(2,3,4); // Calls the add method for adding three integers

int a[] = new int[] {1,2,3,4,5};

sum = myMathClassObject.add(a,5); // Calls the method to add n integers



Now that’s polymorphism for you. Polymorphism itself is of two types – static and dynamic. The one explained above is static polymorphism. ie the binding between the instance variable ( in this case, myMathClassObject ) and the method calls is done during the code compile time. Hence the name static polymorphism.

There is another type of polymorphism – dynamic polymorphism where the binding is done during runtime or dynamically. But, that’s for a different post. Between, Hope this helps.


The Nonsense Blogger


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s