Polymorphism In Java With Example Programs

By | | Updated : 2021-11-02 | Viewed : 63 times

Polymorphism In Java With Example Programs

Polymorphism is one of the main features of OOPs. It is very important to know the basics of Polymorphism in java. So we will try to explore the basics of Polymorphism.

What Is Polymorphism?

Polymorphism is the main feature of OOPs. It is interlinked with inheritance. This means when multiple classes exist with inheritance then it is possible to exist the polymorphism. If the language does not have this feature then it can not be considered an Object-Oriented Programming language.

Polymorphism can be defined by an object\'s ability. Polymorphism allows for performing the actions in different ways. In another way to define polymorphism, it allows to Object to which method to be selected for execution.

What Is Polymorphism In Java?

Polymorphism in Java is also used for performing the same type of action in different ways. An example of Polymorphism is overloading and overriding. Overloading is an example of static polymorphism. Overriding is an example of dynamic polymorphism. We will learn here what are those.

We will first div into the topic of Static and dynamic polymorphism.

Types Of Polymorphism

We will first div into the topic of types of Polymorphism. We have two types of Polymorphism. One is Static and the second is dynamic polymorphism.

Static Polymorphism

Static Polymorphism is also called early binding, A call from an object to the method can be resolved by the compiler in compile time. So that\'s why this type of Polymorphism is called Static Polymorphism. An example of Static Polymorphism is overloading.

Dynamic Polymorphism

Dynamic Polymorphism is also called late binding. So here call to the method will be resolved in run time by JVM. Thus it is called late binding (Dynamic Polymorphism). Method Overriding is an instance of Dynamic Polymorphism.

Now we will look at Method Overriding and Method Overloading with examples.

Method Overriding In Java

Method overriding means the method declaration of the subclass is the same as the superclass but the method body will be different in the subclass. The method body is overridden in a subclass of the superclass.

Please refer to the given example for method overriding in java.

Vehicle.java
public class Vehicle {
    public void run(){
        System.out.println("Vehicle is running");
    }
}
Car.java
public class Car extends Vehicle {
    public void run(){
        System.out.println("Car is running");
    }
}

Notice method resolution depends upon object reference. So If you subclass's object then the subclass method will be invoked.

Method Overloading In Java

Method overloading means the same type of method may take any number of parameters. Then this type of method is called as the method is overridden. It is early binding means method resolution decided by the compiler in compile time. Please refer to the below given example for method overloading in java.

Vehicle.java
package com.docsconsole.tutorials.overloading;

public class Vehicle {
    public void run() {
        System.out.println("Vehicle is running");
    }
}
Car.java
package com.docsconsole.tutorials.overloading;


public class Car extends Vehicle {

    public void run() {
        System.out.println("Car is running");
    }
    public void run(String brand) {
        System.out.println("Car is running and brand:"+brand);
    }

}
MainClient.java
public class MainClient {
    public static void main(String[] args) {
        Car car = new Car();
        car.run("audi");
    }
}

When you execute the main client you can see the Car method will be executed. As you know it is early binding, call resolution will decide at compile time. So Car\'s method will be executed.

Why Do We Need Polymorphism In Java?

Polymorphism allows Objects to perform a single action in multiple ways. Consider overriding when calling the same type of method it will be invoked based on the object type. so here one method will be having different types of forms which is nothing but Polymorphism. If you use a subclass object then the subclass\'s method will be invoked. If you use a superclass object then you can invoke the superclass method. The same method can be invoked and used in different ways. This is called polymorphism.

Advantages Of Polymorphism

Advantages Of Polymorphism

  • It will be applicable to methods in Java for performing single action in different ways.

  • It allows an object to decide to call the method in runtime.

Leave A Reply