Java Polymorphism Tutorial

In this section, we will learn what the polymorphism is and how it works in Java.

Java What is polymorphism?

Polymorphism means “many forms” and it’s a concept that occurs when classes inherit members from each other and define their own version of those members.

For example, consider a method named `jobDescription()` in a base class. Let’s say if we call the method, we will get the message “I’m a manager” on the screen.

On the other hand, a subclass derived from this base class and also overrode the `jobDescription` method. So anytime we create an object from the subclass and call the same method, we will get another message like “I’m a developer”.

This is an example of the polymorphism concept!

In essence, we can create multiple subclasses and all derive from the base class; inside those subclasses we can override the methods of the base class. So anytime we create an object from each of these subclasses and try to access the methods, we will get a different result. Basically, there are multiple methods with the same name but each has different form and hence polymorphism.

Example: method overriding

Create a class named `Person` with this body:

public abstract class Person {
    public abstract void sex();
}

Create a class named `Male` with this body:

public class Male extends Person {
    @Override
    public void sex() {
        System.out.println("I'm a man.");
    }
}

Create a class named `Female` with this body:

public class Female extends Person {

    @Override
    public void sex() {
        System.out.println("I'm a woman.");
    }
}

Now let’s create objects from `Male` and `Female` classs:

public class Simple {
    public static void main(String[] args) {
        Person p1 = new Female();
        Person p2 = new Male();
        p1.sex();
        p2.sex();
    }
}

Output:

I'm a woman.

I'm a man.

As you can see, the `sex()` method is the same in both classes (Male and Female classes). But the body of this method in each class is different. So when we create an object from each of these classes and call this method via both objects, we will get a different form of the method.

Method overloading in Java

Another example of polymorphism is when there are multiple methods with the same name in one class. Basically, polymorphism also happens when we overload a method in a class.

Example: Method overloading

package com.example.demo;

public class Simple {
    public void add(int i, int b){
        System.out.println(i+b);
    }
    public void add(double i, double b){
        System.out.println(i+b);
    }
}


package com.example.demo;

public class Main {

    public static void main(String[] args) {
        Simple simple = new Simple();
        simple.add(20,30);
        simple.add(545.23,23.2342);
    }
}

Output:

50

568.4642

 

As you can see, we have one name `add` set for two methods and depending on the type of values we put as the argument of this method, either of these two methods will be invoked.

This means using the method-overloading, we can create polymorphism in Java as well.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies