Polymorphism in Java

Polymorphism in Java

Polymorphism is a major feature of object-oriented programming.

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

💭 What is Polymorphism?

Polymorphism means representing more than one form with the same name.

Polymorphism is considered one of the important features of Object-Oriented Programming. Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations.

We know Object-Oriented programming is inspired by real-world objects. In real life, the same person can have multiple roles and each role has specific responsibilities. For example, a person can be a father, a brother, a friend, and a manager at the same time. But his responsibilities may differ.

Types of polymorphism

Polymorphism.png

Polymorphism is divided into two types:

  • Compile Time Polymorphism
  • Run -Time Polymorphism

Compile Time Polymorphism

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

Operator Overloading

  • Addition Operator int sum = 2+3;
  • Concatenation Operator System.out.println("Hello"+"World");

Method Overloading

Method overloading occurs when there is more than one method with the same name but different method signatures.

Why do we need method signature?

The compiler uses the method signature to uniquely identify a method during method overloading. The method name along with the number and datatype of parameters is termed as a method signature.

Slide 4_3 - 1.png

Example:

Let us consider a program to find the area of a square, rectangle and circle implemented using method overloading.

class Demo
{
    public static void Area(int side){
        System.out.println("Area of square = "+ side*side);
    }

    public static void Area(int l, int b){
        System.out.println("Area of rectangle = "+ l*b);
    }

    public static void Area(double r){
        System.out.println("Area of circle = "+ 3.14*r*r);
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        Area(5);
        Area(5,6);
        Area(3.0);
    }
}

Runtime Polymorphism

It is also known as Dynamic Polymorphism. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding.

Method Overriding

If a subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. That parent method is said to be overridden.

The main advantage of method overriding is that the class can give its own specific implementation to an inherited method without even modifying the parent class code

Example:

Let us understand this with an example. Consider a program to calculate the GPA of the semester. The way GPA is calculated differs according to the university so we need a specific implementation based on the university, in such scenarios we could use method overriding.

class Education{
    public void displayGPA(){
        System.out.println("GPA is calculated on a scale of 10");
    }
}

class UniveristyOne extends Education{
    public void displayGPA(){
        System.out.println("UniveristyOne caculates the GPA on a scale of 5");
    }
}

class UniversityTwo extends Education{
    public void displayGPA(){
        System.out.println("UniversityTwo calculates the GPA on a scale of 10");
    }
}

class WorkAtTech
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Education A = new UniveristyOne();
        A.displayGPA();
        A=new UniversityTwo();
        A.displayGPA();
    }
}

In the above example, the child class can implement their own version of displayGPA() without affecting the code in the parent class.

Hope this article was helpful.

Feel free to connect with me or just say 👋 Hi

#java#object-oriented-programming#functions
 
Share this