What is Polymorphism in Java?

In this tutorial, we will learn what is Polymorphism in Java. We will understand the basic concept and also learn the different ways to implement it in a Java program.

What is Polymorphism in Java?

Polymorphism is a concept in Java that simply means “more than one form”. In Java, an operator or an object or a method can perform more than one operation.

what is polymorphism in java

The concept of Polymorphism is used in many aspects of Java programming. Let’s discuss them one by one.

Method names in a class

In Java, Polymorphism is implemented regarding method names. There can be two or more methods with the same name within the same class and the same scope. This is possible in Java with a condition.

Either one of the below conditions should satisfy for naming two or more methods with the same name:
The number of input parameters (arguments) of those methods should be different.
In the case where there are the same numbers of input parameters, the order of data type taken as input to those methods should not be identical.

Example:

class Program {

    static void printAboutIt(int a) {
        System.out.println("A method with 1 argument");
    }

    static void printAboutIt(double a) {
        System.out.println("A method with 1 argument of type double");
    }

    static void printAboutIt(int a, int b) {
        System.out.println("A method with 2 argument");
    }

    public static void main (String aggss[]) {
        printAboutIt(2);
        printAboutIt(2.6445654);
        printAboutIt(43,6);
    }
}

/* Output
A method with 1 argument
A method with 1 argument of type double
A method with 2 argument
*/

In the above example, we see that there are 3 methods with the same name within the same class. Each of them has some difference with their input parameters, that way they are being distinguished. Ultimately, we are calling them by the same name. Polymorphism!

Method names in multiple classes in Java

Two or more classes can also have methods with the same name. There are situations where the developer will have an easy time remembering the names of methods due to the concept of polymorphism. Let’s understand it better with examples.

Let’s take a scenario where there is no concept of polymorphism:

class UltraMusicPlayer {
    void ultraMusicPlayerPlay() {
        System.out.println("Music Player is playing");
    }

    void ultraMusicPlayerStop() {
        System.out.println("Music Player stopped playing");
    }

    void ultraMusicPlayerMute() {
        System.out.println("Music Player - Mute");
    }
}

class NationalSuperRadio {
    void nationalSuperRadioPlay() {
        System.out.println("Radio is playing");
    }

    void nationalSuperRadioStop() {
        System.out.println("Radio stopped playing");
    }

    void nationSuperRadioMute() {
        System.out.println("Radio - Mute");
    }
}

class Main {
    public static void main (String aggss[]) {
        UltraMusicPlayer ump = new UltraMusicPlayer();
        NationalSuperRadio nsr = new NationalSuperRadio();

        ump.ultraMusicPlayerPlay();
        ump.ultraMusicPlayerStop();
        nsr.nationalSuperRadioPlay();
        nsr.nationalSuperRadioStop();
        nsr.nationSuperRadioMute();
    }
}

/* Output
Music Player is playing
Music Player stopped playing
Radio is playing
Radio stopped playing
Radio - Mute
*/

In the above example, we see the names of the methods used in both the classes UltraMusicPlayer and NationalSuperRadio are not so developer-friendly to remember. It may be easy to remember for some developers but it might get complicated it was not just for playing or pausing music.

Such methods can be named identical in multiple classes due to polymorphism. Now, let us see an example with polymorphism activated!

class UltraMusicPlayer {
    void play() {
        System.out.println("Music Player is playing");
    }

    void stop() {
        System.out.println("Music Player stopped playing");
    }

    void mute() {
        System.out.println("Music Player - Mute");
    }
}

class NationalSuperRadio {
    void play() {
        System.out.println("Radio is playing");
    }

    void stop() {
        System.out.println("Radio stopped playing");
    }

    void mute() {
        System.out.println("Radio - Mute");
    }
}

class Main {
    public static void main (String aggss[]) {
        UltraMusicPlayer ump = new UltraMusicPlayer();
        NationalSuperRadio nsr = new NationalSuperRadio();

        ump.play();
        ump.stop();
        nsr.play();
        nsr.stop();
        nsr.mute();
    }
}

/* Output
Music Player is playing
Music Player stopped playing
Radio is playing
Radio stopped playing
Radio - Mute
*/

Can u feel the simplicity of method names in the above example? That’s polymorphism.

Use of + operator in different scenarios in Java

As we have seen method names in the context of polymorphism, we have understood one thing that one method name can have different outcomes in different scenarios. In the same way, there is a well-known “+” (plus) operator in Java that acts differently in different scenarios.

Generally, the + operator is used in arithmetic calculations for adding purposes. But the same operator is also used to increment the value by 1 for variables and also to concatenate strings.

Example:

class Program {
    public static void main (String aaggss[]) {
        int v1 = 353;
        int v2 = 865;

        //+ operator used for addition
        System.out.println(v1+v2);

        // + operator used for incrementing a value;
        int value = 100;
        System.out.println(++value);

        // + operator used for concatinating strings
        System.out.println("Hello " + "there!");
    }
}

/* Output
1218
101
Hello there!
*/

I hope now you have a clear understanding of the concept known as polymorphism in Java. Thank you for reading.