Polymorphism in Java | Types of Polymorphism in Java with Examples | Method Overloading vs Overriding in Java

In this tutorial, we will be discussing the most important concept of Java ie., Polymorphism. Knowing each and every bit about the polymorphism in java is very crucial for beginners & experienced coders. So, check out this page without missing any of the links available below. Let’s start with understand what is java polymorphism?

Java Polymorphism

Polymorphism in Java is a concept which enables you to do a single action in various ways. It is the ability of an object or method to take many forms according to requirements. One of the most important features of object-oriented programming (OOPs) is Polymorphism. It is a combination of 2 Greek words: poly and morph. The poly word signifies many and morphs words mean forms. Therefore, when one thing has many forms it is called Java Polymorphism.

Let’s consider a scenario where Car is a class that has a method speed(). Yet, the speed of the car may change according to cars. For instance, the Maruti car has a speed of 60 Km/h, the Alto car has a speed of 70 km/h, and the Brezza car has a speed of 80 km/h. So here is a single method called speed() but their behavior is different according to cars. This is known as polymorphism in Java.

Polymorphism in Java 1

Types of Polymorphism in Java

There are two types of polymorphism in Java:

  1. Static Polymorphism.
  2. Dynamic Polymorphism.

Compile-time polymorphism is also recognized as static polymorphism. Method Overloading is a way to implement compile-time polymorphism.

Runtime Polymorphism is also called Dynamic Polymorphism. Method Overriding is a method to perform the run-time polymorphism.

Polymorphism in Java 2

Also Check:

1. Static or Compile-time Polymorphism in Java:

This compile-time type of polymorphism is also known as static polymorphism. It is achieved by function overloading or operator overloading. But, Operator Overloading is not supported by java.

Method Overloading in Java:

Methods are said to be overloaded when many methods in a class having the same name but different parameter lists. It is similar to Constructor Overloading in Java, which allows a class to have more than one constructor having the same name but having different parameter lists. It increases the readability of the program.

Suppose we have a class Addition and you want to perform the addition of the given number, but there can be any number of parameters. If you write a method such as add2(int x, int y) for two parameters, and add3(int x, int y, int z) for three parameters, then it may be difficult for you and others to understand the behavior of the methods because of its name differs.

Ways to overload a method:

There are three ways to overload a method in Java.

  1. By changing the number of parameters.
  2. By changing the data type.
  3. By changing the sequence of the data type.

Example of Method Overloading: changing the number of parameters.

In this instance, we are going to show you how we can overload a method by changing the number of parameters. In this example, we have created two methods named as add(), first, add() method performs the addition of two numbers, and the second add() method performs the addition of three numbers.

class Addition{
public int add(int x, int y){
return x+y;
}
public int add(int x, int y, int z){
return x+y+z;
}
}
class Person{
public static void main(String args[]){
Addition obj = new Addition();
System.out.println("The Addition of two numbers is : " +obj.add(10,20));
System.out.println("The Addition of three numbers is : " +obj.add(10,20,30));
}
}

Output:

Polymorphism in Java 3

Example of Method Overloading: changing the data type of a parameter.

In this example, we are going to show you how we can overload a method by changing the data type of arguments. In this example, we have created two methods named as add(), But that method differs in data type. The first method calculates the addition of two integer value and the second method calculate the addition of two double value.

class Addition{
public int add(int x, int y){
return x+y;
}
public double add(double x, double y, double z){
return x+y+z;
}
}
class Person{
public static void main(String args[]){
Addition obj = new Addition();
System.out.println("The Addition of two numbers is : " +obj.add(10,20));
System.out.println("The Addition of three numbers is : " +obj.add(10.01, 20.02, 30.04));
}
}

Output:

Polymorphism in Java 4

Example of Method Overloading: changing the sequence data type.

In this example, we are going to show you how we can overload a method by changing the sequence of a data type. In this example, we have created two methods named as a display() with a different sequence of the data type of arguments list. The first method having the arguments (int, double), and the second method having the arguments (double, int).

class Addition{
public void display(int x, double y){
System.out.println("Defination of first method");
}
public void display(double x, int y){
System.out.println("Defination of second method");
}
}
class Person{
public static void main(String args[]){
Addition obj = new Addition();
obj.display(5, 4.5);
obj.display(2.5, 5);
}
}

Output:

Polymorphism in Java 5

Note: Method Overloading in Java is not possible by changing the return type of the method only because of the ambiguity problem. Let’s see how ambiguity may occur.

class Addition{
public int add(int x, int y){
return x+y;
}
public double add(int x, int y){
return x+y;
}
}
class Person{
public static void main(String args[]){
Addition obj = new Addition();
System.out.println(obj.add(4,5)); //Ambiguity may occur
}
}

In this example, we have created two methods named add(), But having a different return type. When we call this method Java compiler show compile-time error, because how can the Java compiler determine which add() method should be called?

2. Dynamic or Runtime Polymorphism in Java

Dynamic polymorphism is also known as runtime polymorphism. Method Overriding is a way to implement runtime polymorphism in Java.

Method Overriding in Java:

Method Overriding is a feature that allows you to redefine the parent class method in the child class based on its requirement.

In other words, whatever methods parent has by default available to the child through inheritance. Sometimes a child may not satisfy with parent methods implementation. Then the child is allowed to redefine that method based on its requirements, this process is called method overriding.

  • The parent class method which is overridden is known as an overridden method.
  • The child class method which is overriding is known as an overriding method.

The main purpose of method overriding is that the class gives its own implementation to an inherited method without even modifying the parent class code.

Rules for Method Overriding in Java:

  • The method of the parent class and the method of child class must have the same name.
  • The method of the parent class and the method of child class must have the same parameter.
  • Method Overriding in Java is possible through inheritance only.

Therefore to understand the concept of method overriding you should have the basic knowledge of Inheritance in Java

Example of Java Method Overriding:

Let’s take a simple example to understand the concept of method overriding.
We have two classes: A parent class Animal and a child class Dog. The Dog class extends the Animal class. Both the class have a common method eat(). The Dog class is giving its own implementation to the eat() method. It is an overriding eat() method.

The child class gives its own implementation so that when it calls this method, it prints Dog is eating instead of Animal is eating.

class Animal{
public void eat(){
System.out.println("Animal is eating");
}
}
class Dog extends Animal{
public void eat(){
System.out.println("Dog is eating");
}
}
class Person{
public static void main(String args []){
Dog obj = new Dog();
obj.eat();
}
}

Output:

Dog is eating

Overloading vs Overriding in Java:

  1. Overloading is also known as compile-time polymorphism or static polymorphism or early binding while overriding is also called runtime polymorphism or dynamic polymorphism or late binding.
  2. The main purpose of overloading is to enhance the readability of the program while overriding the class gives its own implementation to an inherited method without even modifying the parent class code.
  3. Overloading occurs at compile-time whereas Overriding occurs at runtime.
  4. Overloading is made in the same class whereas Overriding inheritance is required.
  5. In overloading, the parameter needs to be different while in overriding the parameter must be the same.
  6. Private, static, and final methods can be overloaded but cannot be overridden.
  7. The return type can be the same or different in overloading while the return type must be the same or covariant return type in the overriding.

Leave a Comment