Polymorphism in Java

Polymorphism in Java is the ability to create member functions or fields that behaves differently in different programmatic contexts. It is one of the major building blocks of object-oriented programming, along with inheritance, abstraction and encapsulation.

1. What is Polymorphism?

Polymorphism allows a class to act differently in different contexts. We can relate the polymorphism with one interface having multiple implementations. Though the contract defined by the interface remains the same, each class implements the contract differently and thus exhibits different behavior. This is polymorphic behavior.

For example, polymorphism is also referring the instance of a subclass, with a reference variable of the superclass.

Object o = new Object(); //o can hold the reference of any subtype

Object o = new String();
Object o = new Integer();

Here, String and Integer classes are the subclasses of the Object class.

2. Types of Polymorphism

In Java language, polymorphism is essentially considered into two forms:

  • Compile time polymorphism (static binding or method overloading)
  • Runtime polymorphism (dynamic binding or method overriding)

Here, it is important to understand that these divisions are specific to java. In the context of software engineering, there are other forms of polymorphisms also applicable to different languages, but for java, these two are mainly considered.

2.1. Compile Time Polymorphism

As the meaning is implicit, in compile time polymorphism, the flow of control is decided in compile time itself. It is achieved using method overloading.

In method overloading, an object can have two or more methods with the same name, but the method parameters are different such that:

  • number of parameters are different
  • method parameter types are different

For example, in the Calculator class, we can have multiple methods with different argument types:

public class Calculator {

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

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

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

With the above class definition, when we invoke the sum() method in the program, based on argument types, the compiler decides which method to call on the compile time only, and generates the bytecode accordingly. This is called compile-time polymorphism.

Calculator calc = new Calculator();

Integer sum1 = calc.sum(1 ,2);
Float sum2 = calc.sum(1f ,2f);
Double sum3 = calc.sum(1d ,2d);

2.2. Runtime Polymorphism

Runtime polymorphism is essentially referred as method overriding when we extend a class into a child class. In runtime polymorphism, which method will be invoked is decided on the runtime based on the actual instance of the child class.

A simple example from real-world can animals. An application can have Animal class, and its specialized subclasses like Cat and Dog. These subclasses will override the default behavior provided by Animal class, and additionally, some of its own specific behavior.

class Animal {

  public void makeNoise() {
    System.out.println("Some sound");
  }
}

class Dog extends Animal {

  public void makeNoise() {
    System.out.println("Bark");
  }
}

class Cat extends Animal {

  public void makeNoise() {
    System.out.println("Meawoo");
  }
}

Now when we invoke the makeNoise() method, the method called depends on type of actual instance created on runtime.

Animal cat = new Cat();
cat.makeNoise(); //Prints Meowoo

Animal dog = new Dog();
dog.makeNoise(); //Prints Bark

3. Conclusion

  • Polymorphism is the ability to create a variable, function, or object with more than one form.
  • In java, polymorphism is divided into method overloading and method overriding.
  • Another term, operator overloading, is also there. For example, the “+” operator can be used to add two integers as well as concat two sub-strings. Well, this is the only available support for operator overloading in java, and you can not have your own custom-defined operator overloading in java.

Happy Learning !!

Leave a Reply

17 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial