Polymorphism in Java – OOP – Code Example

In previous posts we have discussed many important topics of object oriented programming like classes, objects, inheritance etc. In this post we will discuss polymorphism in Java which is another important OOP concept.

What is Polymorphism in Java?

The word polymorphism means ‘a state of having many shapes’. In terms of object oriented programming it’s the ability to process objects of various types and classes through a single uniform interface. An object, variable or function may take on multiple forms / shapes and this phenomenon is known as Polymorphism.

Example of Polymorphism in JAVA:


public int add(int a, int b){

   return a+b;

}

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

   return a+b+c;

}

In above example we can see that the add function have 2 shapes/forms with varying number of input arguments. This is an example of Polymorphism.

Types of Polymorphism in JAVA:

There are two types of Polymorphism in JAVA namely static polymorphism and dynamic polymorphism.

Static Polymorphism in JAVA:

Static polymorphism is achieved through method overloading and is also known as compile time polymorphism or static binding. At compile time JAVA decides which method to call by checking the signature/prototype of the method. Following code example will make the concept clear.


//file name is Calculate.java

Class Calculate{

   public int addNumbers(int a, int b){ //method 1

      return a+b;

   }

   public int addNumbers(int a, int b, int c){ //method 2

      return a+b+c;

   }

   public int addNumbers(double a, int b){  // method 3

      return (int)x+y;

   }

}

//main class Main.java

Class Main{

   public static void main(String[] args){

      Calculate c1 = new Calculate();

      System.out.println(c1.addNumbers(4+5));   // method 1 will be called

      System.out.println(c1.addNumbers(4.5+16));  // method 3 will be called

      System.out.println(c1.addNumbers(3+6+12));  // method 2 will be called

   }

}

Dynamic Polymorphism in JAVA:

Dynamic polymorphism is achieved through method overriding and is also known as dynamic binding or late binding. Let’s say parent class (Human) has a method named as run() and child class (Student) overrides that method run(). Now considering the class reference Java decides which method to call. Following example will elaborate the concept.


//Human class: Human.java

Class Human{

   public void run(){

      System.out.println("Human is running");

   }

}

//Student class: Student.java

Class Student extends Human{

   public void run(){

      System.out.println("Student is running");

   }

}

//Main class: Main.java

Class Main{

   public static void main(String[] args){

      Human h1 = new Human();

      h1.run();    // prints 'Human is running'

      h1 = new Student();   // polymorphism

      h1.run();  // prints 'Student is running'

   }

}

In above example h1 is an object of Human class so when we called the run() method Java will call the run() method of Human class. Then we changed the h1 object’s reference to Student class (new shape/form which is polymorphism). Now Java will call the run() method of Student class because now the object’s reference is changed to Student class.

7 Replies to “Polymorphism in Java – OOP – Code Example”

  1. polymorphism is about two or more than two functions having same name and different data type and different sequence ,is this true sir???and what is the benefit of poly…?

    1. Code reuse-ability, having multiple data type variables/functions/objects with same name but different parameters are some of the advantages of polymorphism.

    1. Because by default the decimal number type is double in java so at this point

      System.out.println(c1.addNumber(4.5+16));
      java gonna consider 21.12 values is a double not float hence java look-up table try to find the best match with double value

      The float literals as marked with either f or F so to call function with floating values do this
      System.out.println(c1.addNumbers(4.5f+16)); // method 3 will be called

Leave a Reply

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