What is polymorphism in Java OOP

Upasana | May 24, 2019 | 2 min read | 1 views


Polymorphism

It means one name many forms. It is further of two types - static and dynamic. Static polymorphism is achieved using method overloading and dynamic polymorphism using method overriding.

Static polymorphism

Static polymorphism is achieved using method overloading. Method overloading is a programming technique that allows developers to use the same method name multiple times in the same class, but with different parameters.

There are mainly two variations of method overloading:

  1. implementing two or more methods that have the same name but take different numbers of arguments

  2. implementing two or more methods that have the same name but take arguments of different types

Example

Calculator.java
public class Calculator {

    public int sum(int a, int b) {
        return a + b;
    }

    public int sum(int a, int b, int c) {
        return a + b + c;
    }
}

As we can see that method overloading makes code clean and easier to read.

Dynamic polymorphism

Dynamic polymorphism is achieved in Java using method overriding. It is closely related to inheritance. We can write a code that works on the superclass, and it will work with any subclass type as well.

Example

Java collections framework has an interface called java.util.Collection, ArrayList and TreeSet are two different implementation of this interface. ArrayList maintains the insertion order of elements while TreeSet orders its elements by their natural order or comparator(if supplied). Now if we write a method that accepts a collection and prints its elements, the actual object (ArrayList or TreeSet) at runtime will decide the behavior of this method.

Polymorphic print method
public void print(Collection<String> collection) {
    for (String s : collection) {
        System.out.println("s = " + s);
    }
}
Passing an ArrayList
Collection<String> collection1 = new ArrayList<>();
collection1.add("A");
collection1.add("D");
collection1.add("B");
collection1.add("C");
print(collection1); (1)
1 elements will be printed as per the insertion order of elements into arraylist
Program output
s = A
s = D
s = B
s = C
Passing an TreeSet
Collection<String> collection2 = new TreeSet<>();
collection2.add("A");
collection2.add("D");
collection2.add("B");
collection2.add("C");
print(collection2);     (1)
1 elements will be printed as per the natural order
Program output
s = A
s = B
s = C
s = D

We just saw that print() method’s behavior is determined by the actual type of object passed to it at run time. That’s polymorphism!

Important Facts
  1. Other than objects of type java.lang.Object, all java objects are polymorphic i.e. they pass the IS-A test for their own type as well as for class Object.

  2. A reference variable’s type determines the methods that can be invoked on the object that variable is referencing to. In the example above, print() method can only invoke methods that are listed on Collection interface irrespective the type of actual object passed to this method.

  3. Polymorphic method invocation applies only to the instance methods (not to static methods, not to variables). Only overriden instance methods are dynamically invoked based on the real object’s type at runtime.


Top articles in this category:
  1. What are four principles of OOP, How aggregation is different than Composition?
  2. Is Java Pure Object Oriented Language?
  3. What do you understand by Java Memory Model?
  4. What is difference between sleep() and wait() method in Java?
  5. What is purpose of Collections.unmodifiableCollection
  6. What is volatile keyword in Java
  7. What is Immutable Class in Java

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.