Polymorphism in Java with Example

In this chapter, we are going to look at examples and explanations of Polymorphism in Java.

As you all know, polymorphism simply means “different forms”. In Java, it means that we can perform a single action in many different ways.

Polymorphism in Java is something related to Inheritance as well.

Let’s understand, as you know in our previous chapter Inheritance, we discussed that Inheritance is a process where one class can inherit the properties and functionalities of another class.

In polymorphism, we use those methods and properties to perform a different task.

For instance, one method can have multiple functionalities in a different situation. Let’s take an example:

Suppose, there is a class that defines animal sound then every animal can inherit that class and make their own voice.

class Animal {
    public void animalSound() {
        System.out.println("The animal makes a sound");
    }
}
class Horse extends Animal {
    @Override public void animalSound() {
        System.out.println("The horse says: Neigh");
    }
}
class Dog extends Animal {
    @Override public void animalSound() {
        System.out.println("The dog says: bow bow");
    }
}

There are two types of Polymorphism in Java:

1. Runtime Polymorphism In Java (Method Overriding)

We can achieve dynamic polymorphism during Inheritance only because the method declared in the parent class is overridden by the subclass to provide a custom implementation.

It is better to use @Override annotation while overriding a method.

Example:

public class ParentClass {
	
	public void printMethod() {
		System.out.println("Printing From Parent Class");
	}
}

class ChildClass extends ParentClass {
    //overridden method of parent class with custom implementation 
    @Override public void printMethod() {
        System.out.println("Printing From Child Class");
    }
    public static void main(String args[]) {
        ChildClass obj = new ChildClass();
        obj.printMethod();
        //printing from child class
    }
}

2. Compile Time Polymorphism In Java (Method Overloading)

Method Overloading or static polymorphism is an example of Compile time polymorphism.

Because in method overloading we can overload a method within the same class on the basis of the different number of parameters or different data type of parameter.

Example 1: Different Parameters

class AdditionClass {
    public int addNumbers(int a, int b) {
        return a + b; // return addition of two numbers 
    }
    public int addNumbers(int a, int b, int c) {
        return a + b + c; // return addition of three numbers
    }
    public int addNumbers(int a, int b, int c, int d) {
        return a + b + c + d; // return addition of four numbers 
    }
    public static void main(String args[]) {
        AdditionClass obj = new AdditionClass();
        System.out.println(obj.addNumbers(1, 2));
        System.out.println(obj.addNumbers(1, 2, 3));
        System.out.println(obj.addNumbers(1, 2, 3, 4));
}

Example 2: Different Data Type of Parameter

class ClassA {
    public void printValue(int value) {
        System.out.println(value);
    }
    public void printValue(String value) {
        System.out.println(value);
    } // Method names are still the same but data types are different 
    public static void main(String args[]) {
        ClassA obj = new ClassA();
        obj.printValue(1);
        obj.printValue("print string value");
    }
}

Points To Remember:

  1. To reduce code duplicacy we use polymorphism concepts.
  2. Always use @Override annotation while overriding any method.
  3. We can’t override private, static, and final methods because they are not accessible outside the class.
  4. One can give their custom implementation by overriding any method without changing the parent class code.

I hope you’ve understood what is Polymorphism in Java.