Guide to Polymorphism

In simple words, polymorphism is the ability by which, we can create functions or reference variables which behaves differently in different programmatic context.

Polymorphism is one of the major building blocks of object oriented programming along with inheritance, abstraction and encapsulation.

“Subtype polymorphism, often referred to as simply polymorphism in the context of object-oriented programming, is the ability to create a variable, a function, or an object that has more than one form.” – Wikipedia

Recommended Reading : Java Abstraction vs Encapsulation.

Polymorphism in Java

An example of polymorphism is referring the instance of subclass, with reference variable of super-class. e.g.

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

Here, String is subclass of Object class. This is basic example of polymorphism.

In java language, polymorphism is essentially considered into two versions.

  1. Compile time polymorphism (static binding or method overloading)
  2. Runtime polymorphism (dynamic binding or method overriding)

Compile Time Polymorphism (static binding or method overloading)

As the meaning is implicit, this is used to write the program in such a way, that 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 same name, BUT, with their method parameters different. These parameters may be different on two bases:

Parameters type

Type of method parameters can be different. e.g. java.util.Math.max() function comes with following versions:

public static double Math.max(double a, double b){..}
public static float Math.max(float a, float b){..}
public static int Math.max(int a, int b){..}
public static long Math.max(long a, long b){..}

The actual method to be called is decided on compile time based on parameters passed to function in program.

Parameters count

Functions accepting different number of parameters. e.g. in employee management application, a factory can have these methods:

EmployeeFactory.create(String firstName, String lastName){...}
EmployeeFactory.create(Integer id, String firstName, String lastName){...}

Both methods have same name “create” but actual method invoked will be based on parameters passed in program.

Runtime Polymorphism (dynamic binding or method overriding)

Runtime polymorphism is essentially referred as method overriding. Method overriding is a feature which you get when you implement inheritance in your program.

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

public 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 which makeNoise() method will be called, depends on type of actual instance created on runtime e.g.

public class Demo
{
public static void main(String[] args) {
Animal a1 = new Cat();
a1.makeNoise(); //Prints Meowoo

Animal a2 = new Dog();
a2.makeNoise(); //Prints Bark
}
}
Here, it is important to understand the these divisions are specific to java. In context to software engineering, there are other form of polymorphisms also applicable to different languages, but for java, these are mainly considered.

Important points

  1. Polymorphism is the ability to create a variable, a function, or an object that has more than one form.
  2. In java, polymorphism is divided into two parts : method overloading and method overriding.
  3. Some may argue that method overloading is not polymorphism. Then what does the term compile time “polymorphism” means??
  4. Another term operator overloading is also there, e.g. “+” 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

42 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.