Polymorphism in Java

Introduction

Greetings! In today's lesson, we'll unravel the concept of polymorphism in Java's Object-Oriented Programming (OOP). Grasping polymorphism enables us to use a single entity (a method, class, or interface) to represent different types in various scenarios. Let's proceed.

Seeing Polymorphism in Action

Polymorphism, a pillar of OOP, allows one object to embody multiple forms. Visualize a button in software; depending on its type (for instance, a submit button or a radio button), the action resulting from pressing it varies. This dynamic encapsulates the spirit of polymorphism!

Java supports various types of polymorphism. Run-time polymorphism, also known as dynamic polymorphism, occurs during runtime and leverages method overriding. Let's observe dynamic polymorphism in action within a simple application involving shapes. The base Shape class has an area method, which calculates the area for shapes. This method is uniquely implemented in the subclasses Rectangle and Circle.

abstract class Shape {
    abstract double area();
}

class Rectangle extends Shape {
    private double length;
    private double width;

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    double area() {
        return length * width;
    }
}

class Circle extends Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    double area() {
        return 3.14 * radius * radius;
    }
}

public class Main {
    public static void main(String[] args) {
        Shape rectangle = new Rectangle(2, 3);
        System.out.println(rectangle.area()); // Prints: 6.0

        Shape circle = new Circle(5);
        System.out.println(circle.area()); // Prints: 78.5
    }
}

Here, polymorphism shines as the area() method takes on multiple forms while using the same Shape abstract class. It behaves differently depending on whether the object is a Rectangle or a Circle.

Method Overloading in Java: Static Polymorphism

The other type of polymorphism that Java supports is a compile-time polymorphism, also known as static polymorphism, which operates during compile time and involves method overloading. Let's look at the example of the static polymorphism:

class MathOperations {
    // Overloaded method for rectangle area
    public double area(double length, double width) {
        return length * width;
    }

    // Overloaded method for circle area
    public double area(double radius) {
        return 3.14 * radius * radius;
    }
}

public class Main {
    public static void main(String[] args) {
        MathOperations mathOps = new MathOperations();
        System.out.println(mathOps.area(5, 2)); // Rectangle area
        System.out.println(mathOps.area(3));    // Circle area
    }
}

In this example, polymorphism is demonstrated via method overloading. The area method has multiple forms, accepting different parameter lists to compute the area of either a rectangle or a circle based on the provided arguments. This is a clear example of static polymorphism, where the correct method is determined at compile time.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal