Skip to the content.

Fullstack > Java > 🎭 Polymorphism

Understanding Polymorphism in Java

Polymorphism is one of the four fundamental Object-Oriented Programming (OOP) concepts in Java, along with Encapsulation, Inheritance, and Abstraction. The term Polymorphism is derived from two Greek words: “Poly” meaning “many” and “Morph” meaning “forms.” This concept allows a single method, function, or operator to take on multiple forms, enabling flexibility and maintainability in Java applications.

Types of Polymorphism in Java

Java supports two types of polymorphism:

  1. Compile-time Polymorphism (Method Overloading)
  2. Runtime Polymorphism (Method Overriding)

1. Compile-time Polymorphism (Method Overloading)

Method Overloading occurs when multiple methods in the same class share the same name but differ in parameters (number, type, or both). The compiler determines which method to call based on the arguments provided.

Example of Method Overloading:

class Calculator {
    // Method with two integer parameters
    int multiply(int a, int b) {
        return a * b;
    }

    // Overloaded method with three integer parameters
    int multiply(int a, int b, int c) {
        return a * b * c;
    }

    // Overloaded method with double parameters
    double multiply(double a, double b) {
        return a * b;
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.multiply(5, 10));        // Calls multiply(int, int)
        System.out.println(calc.multiply(5, 10, 2));   // Calls multiply(int, int, int)
        System.out.println(calc.multiply(5.5, 2.5));    // Calls multiply(double, double)
    }
}

Key Points:


2. Runtime Polymorphism (Method Overriding)

Method Overriding occurs when a subclass provides a specific implementation for a method already defined in its superclass. The method signature remains the same, but the behavior differs in the child class.

Example of Method Overriding:

class Vehicle {
    void speed() {
        System.out.println("Vehicles have different speeds");
    }
}

class Car extends Vehicle {
    @Override
    void speed() {
        System.out.println("Cars can go up to 200 km/h");
    }
}

class Bicycle extends Vehicle {
    @Override
    void speed() {
        System.out.println("Bicycles have an average speed of 20 km/h");
    }
}

public class Main {
    public static void main(String[] args) {
        Vehicle myVehicle1 = new Car(); // Upcasting
        myVehicle1.speed(); // Calls Car's overridden method
        
        Vehicle myVehicle2 = new Bicycle(); // Upcasting
        myVehicle2.speed(); // Calls Bicycle's overridden method
    }
}

Key Points:


Method Overloading vs Method Overriding

Feature Method Overloading Method Overriding
Resolution Time Compile-time Runtime
Method Name Same Same
Parameters Different Same
Return Type Can be different Must be the same (or covariant)
Access Modifier Can be changed Cannot be more restrictive than parent class

```

Key Points:


Conclusion

Polymorphism is a powerful OOP concept that improves code reusability and makes programs more scalable.


🔗 Related Topics: