Polymorphism in Java with examples

Polymorphism in Java refers to an object's capacity to assume various forms. This promotes greater flexibility and code reuse by allowing objects of various classes to be handled as though they were objects of a single superclass.

Method overloading (compile-time polymorphism) and method overriding (runtime polymorphism) are two mechanisms that can be used to achieve polymorphism.

Polymorphism in Java: method overloading

Method overloading is a technique in which multiple methods with the same name but different parameters exist in a class. The method that is executed is determined at compile time based on the arguments that are passed. This is also known as static polymorphism or compile-time polymorphism.

When an overloaded method is called, Java determines which version to call based on the number, types, and order of the arguments. Here's an example that demonstrates method overloading in Java:

Java Code
public class MyClass {
    public int FindSum(int a, int b) {
        return a + b;
    }
    
    public double FindSum(double a, double b) {
        return a + b;
    }
    
    public int FindSum(int a, int b, int c) {
        return a + b + c;
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass myObject = new MyClass();
        
        int result1 = myObject.FindSum(2, 3);         // calls the FindSum(int a, int b) method
        double result2 = myObject.FindSum(2.5, 3.5);  // calls the FindSum(double a, double b) method
        int result3 = myObject.FindSum(2, 3, 4);      // calls the FindSum(int a, int b, int c) method
        
        System.out.println(result1);
        System.out.println(result2);
        System.out.println(result3);
    }
}
Output
5
6.0
9

The MyClass class in the given program has three methods with the same name FindSum, but each method takes a different number and type of parameters.

In the above example, the FindSum method's ability to take on multiple forms depending on the parameters passed to it at runtime is an example of polymorphism in Java via method overloading.

Polymorphism in Java: method overriding

Method overriding is a type of runtime polymorphism in Java in which a subclass provides its own implementation of a method defined in its superclass.

To override a method in a subclass, it must have the same name, return type, and parameter list as the superclass method. The subclass method's access level cannot be stricter than the superclass method's. For example, you cannot override a public method with a private method. Here is an illustration of Java's method overriding:

Java Code
public class MyClass {
    public int FindSum(int a, int b) {
        return a + b;
    }
    
    public int FindSum(int a, int b, int c) {
        return a + b + c;
    }
    
    public static void main(String[] args) {
        MyClass myObject = new MySubClass();
        
        int result1 = myObject.FindSum(2, 3);         // calls the overridden FindSum(int a, int b) method
        int result2 = myObject.FindSum(2, 3, 4);      // calls the FindSum(int a, int b, int c) method from the superclass
        
        System.out.println(result1);
        System.out.println(result2);
    }
}

public class MySubClass extends MyClass {
    @Override
    public int FindSum(int a, int b) {
        return a + b + 10;    // overrides the FindSum(int a, int b) method to add 10 to the result
    }
}
Output
15
9

The superclass MyClass defines two methods with the same name FindSum, but with different parameters. The subclass MySubClass overrides one of these methods by providing a new implementation for the method. When the main method is executed, an object of MySubClass is created and assigned to a variable of type MyClass. This is an example of upcasting.

The FindSum method of MySubClass is called with the arguments (2, 3), which overrides the same method in the superclass, and adds 10 to the result. The FindSum method of MyClass is called with the arguments (2, 3, 4), which is not overridden in the subclass, so the implementation in the superclass is used. The program then prints the two results to the console. This demonstrates the use of polymorphism to call different versions of the same method based on the object's actual type at runtime.

Advantages of polymorphism in Java

Disadvantages of polymorphism in Java

Java Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!