Polymorphism in Java

Polymorphism in Java

Definition

Polymorphism is an Object-Oriented Programming (OOP) concept that allows objects to take multiple forms. The word “polymorphism” comes from Greek: “poly” (many) + “morph” (forms).

In Java, polymorphism allows:

  1. One interface, multiple implementations
  2. Methods to behave differently based on the object that invokes them

It provides flexibility and reusability in code.


Types of Polymorphism in Java

Polymorphism in Java is of two types:

  1. Compile-time Polymorphism (Static Polymorphism)
  2. Runtime Polymorphism (Dynamic Polymorphism)

1. Compile-time Polymorphism (Method Overloading)

Occurs when multiple methods in the same class have the same name but different parameters (number or type).

Example:

class Calculator {

    // Method to add two integers
    int add(int a, int b) {
        return a + b;
    }

    // Method to add three integers
    int add(int a, int b, int c) {
        return a + b + c;
    }

    // Method to add two double numbers
    double add(double a, double b) {
        return a + b;
    }
}

public class TestPolymorphism {
    public static void main(String[] args) {
        Calculator calc = new Calculator();

        System.out.println(calc.add(5, 10));        // Calls add(int, int)
        System.out.println(calc.add(5, 10, 15));    // Calls add(int, int, int)
        System.out.println(calc.add(5.5, 10.5));    // Calls add(double, double)
    }
}

Explanation:
The method add behaves differently depending on the arguments passed. This is compile-time polymorphism, decided during compilation.

2. Runtime Polymorphism (Method Overriding)

Occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.
The method to call is determined at runtime based on the actual object type.

Real-world analogy:

Think of a Bird class. All birds can makeSound(). But a Sparrow chirps and a Parrot talks. The same method name produces different behaviors depending on the bird.

Example:

// Superclass
class Bird {
    void makeSound() {
        System.out.println("Some generic bird sound");
    }
}

// Subclass 1
class Sparrow extends Bird {
    @Override
    void makeSound() {
        System.out.println("Chirp chirp");
    }
}

// Subclass 2
class Parrot extends Bird {
    @Override
    void makeSound() {
        System.out.println("Squawk! Hello!");
    }
}

public class TestPolymorphism {
    public static void main(String[] args) {
        Bird myBird1 = new Sparrow();
        Bird myBird2 = new Parrot();

        myBird1.makeSound();   // Output: Chirp chirp
        myBird2.makeSound();   // Output: Squawk! Hello!
    }
}

Explanation:
Even though both myBird1 and myBird2 are declared as type Bird, the actual object type (Sparrow or Parrot) determines which makeSound() method is called. This is runtime polymorphism.


Benefits of Polymorphism

  1. Code reusability – Write one interface and reuse it with different objects.
  2. Flexibility – New classes can be introduced without changing existing code.
  3. Maintainability – Less coupling between classes.
  4. Simplifies code – You can write generic code that works with different object types.

Key Points to Mention in a Blog Tutorial

  • Polymorphism allows a single method name to perform different tasks.
  • Compile-time polymorphism = method overloading (decided at compile-time).
  • Runtime polymorphism = method overriding (decided at runtime).
  • Always involves inheritance and interfaces in runtime polymorphism.
  • Real-world analogy makes understanding easier (like birds making different sounds).

Java developer with 9+ years of IT experience, sharing tutorials and tips to help learners master Java programming.

Leave a Reply

Your email address will not be published. Required fields are marked *