Polymorphism in Java:How it Works

Polymorphism in Java:How it Works

In the previous blog, we read about Inheritance. In this blog let's deep dive into one of the pillars of Object Oriented Programming called Polymorphism.

What is Polymorphism?

In technical terms, polymorphism means having the ability of a message to be displayed in more than one form.

Real-life Example

We have Cars from different companies, Let's say BMW Car, Audi Car, and Porsche Car. Now if we have to call it then mostly we don't call it by its brand name. We simply call it a car and it can be of a different brand. But in general, we call it a car. So this can be an example of Polymorphism.

In Java polymorphism is mainly divided into two types:

  • Compile-time Polymorphism

  • Runtime Polymorphism

Compile-time Polymorphism

It is also known as static Polymorphism. This type of polymorphism is achieved by function overloading or operator overloading.

Method Overloading:

When more than one method is declared with the same name but with a different number of parameters or a different data type of parameter that is called method overloading.

//Example of Method Overloading
class Base{
    public void box(int x){ }
}
class Dervied extends Base{
    //Overload Method
    public void box(float x) { }
}
  • In the above code, the method box is declared twice but both have different signatures. One is accepting an integer as a value in the parameter and the other is accepting the float value as a parameter.

  • It is also known as static binding as during the compilation time, it's clear which method to call. It is recognized with the help of Data type and the compilation time of the program.

Types of Method Overloading

  1. Function Overloading: It is a feature where multiple functions can have the same name.

Operator Overloading: It is a feature where the operators such as +, -, * etc. can be given additional meanings when applied to user-defined data types.

Runtime Polymorphism

Method Overriding

Method Overriding is basically when more than one method is declared with the same name and same signature but in a different class. These functions or methods are said to be overriding. Method Overriding is also known as Dynamic Method Dispatch.

In Technical terms, it is a process in which a function call to the overridden method is resolved at Runtime.

//Example of Method Overriding.
//Sourced from GeeksForGeeks.
class Parent {
    // Method of parent class
    void Print()
    {
        // Print statement
        System.out.println("parent class");
    }
}
// Class 2
// Helper class
class subclass1 extends Parent {
    // Method
    void Print() { System.out.println("subclass1"); }
}
// Class 3
// Helper class
class subclass2 extends Parent {
    // Method
    void Print()
    {
        // Print statement
        System.out.println("subclass2");
    }
}

In the above Code, When an object of a child class is created, then the method inside the child class is called. This is because The method in the parent class is overridden by the child class. Since The method is overridden, This method has more priority than the parent method inside the child class. So, the body inside the child class is exe

Conclusion

Object Oriented Programming Principle is the most widely used programming paradigm and Polymorphism is an important part of it.

That's all about the Polymorphism in Java. Do leave any feedback if you have.

Did you find this article valuable?

Support Priyanshu Raj by becoming a sponsor. Any amount is appreciated!