Polymorphism in Java

Static and dynamic

Merce Bauza
2 min readFeb 12, 2019

Object-oriented programming is built on four pillars: abstraction, encapsulation, inheritance, and polymorphism.

Those concepts help OOP to benefit of the usage of objects, and as a consequence, create the “best practices” to use them.

As an example, objects could be created with the ability to take on the properties of existing objects (inheritance), hiding away the implementation details using interfaces (abstraction)…

I will be focusing in polymorphism in this blog post.

What is polymorphism?

Polymorphism is a term used to refer at the ability of an object to adopt many forms.

In OOP, polymorphism refers to a programming language’s ability to process objects differently depending on their data type or class.

There are two types of polymorphism in java:
1) Static Polymorphism also known as compile time polymorphism
2) Dynamic Polymorphism also known as runtime polymorphism

Static polymorphism

In Java, static polymorphism is achieved through method overloading.

Method overloading means there are several functions in a class with the same name but different types/order/number of arguments, which will perform differently.

At compile time, Java knows which method to invoke by checking the method signatures.

The concept will be clear from the following example:

class Calculator{

public int sum(int x, int y){ //method 1
return x+y;
}

public int sum(int x, int y, int z){ //method 2
return x+y+z;
}
}

The compiler will know which one to use, depending on the method signature:

Calculator calculator = new Calculator();Int solution = calculator.add(2+5);

In this case, Java will use method 1, as we are only passing two arguments, that matches with the number of arguments of method 1.

Dynamic polymorphism

In Java, static polymorphism is achieved through method overriding.

Method overriding is a feature which you get when you implement inheritance in your program.

It means that your child class can override the default behaviour of a method of its parent class and when the method is called from an instance of the child class, it will have its own specific behaviour.

At runtime time, Java knows which method to invoke by checking the object who is calling it.

The concept will be clear from the following example:

Imagine we have a Finder class, that would like to find objects:

class Finder{public void find(String name){
//find items
}
}

We could create a class ImageFinder to search for images:

class ImageFinder extends Finder{@Override
public void find(String name){
//find images specific code
}
}

Or a VideoFinder for video search:

class VideoFinder extends Finder{@Override
public void find(String name){
//find videos specific code
}
}

Find method will perform differently if the class is VideoFinder or ImageFinder.

videoFinder.find(); //it will find videos imageFinder.find(); //it will find images

Conclusion

Polymorphism, if used correctly, is a very useful feature of Object Oriented Programming

It could help us structure and design our code base more effectively, and following best practices.

--

--