Java Polymorphism

Introduction

Polymorphism is one of the four fundamental pillars of Object-Oriented Programming (OOP) in Java, alongside Encapsulation, Inheritance, and Abstraction. The word Polymorphism is derived from two Greek words: Poly, meaning “many,” and Morph, meaning “forms.” In programming, polymorphism means one interface, many implementations or one method behaving differently in different situations.

Polymorphism allows the same method name or object reference to perform different tasks depending on the context. This makes Java applications more flexible, reusable, and easier to maintain.

For example, consider a draw() method. The same method can draw a circle, rectangle, or triangle depending on the object calling it.

Java supports two types of polymorphism:

  • Compile-time Polymorphism (Static Polymorphism) through Method Overloading
  • Runtime Polymorphism (Dynamic Polymorphism) through Method Overriding

What is Polymorphism?

Polymorphism allows a single interface or method name to represent multiple behaviors.

Example:


class Animal {
    void sound() {
        System.out.println("Animal Sound");
    }
}
class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog Barks");
    }
}

The same sound() method behaves differently depending on the object.

Why Do We Need Polymorphism?

Without polymorphism, developers would need separate methods for every object type.

Example without polymorphism:


playDogSound();
playCatSound();
playCowSound();

Using polymorphism:


animal.sound();

The correct implementation is selected automatically, making the code cleaner and easier to extend.

Types of Polymorphism

Java supports two major types of polymorphism.


Java supports two major types of polymorphism.
           Polymorphism
                 │
      ┌──────────┴──────────┐
      ▼                     ▼
Compile-time          Runtime
(Method Overloading) (Method Overriding)

Compile-time Polymorphism

Compile-time polymorphism is also called:

  • Static Polymorphism
  • Early Binding

It is achieved using Method Overloading.

The compiler determines which method should execute based on the method signature.

Example:


class Calculator {
    int add(int a, int b) {
        return a + b;
    }
    int add(int a, int b, int c) {
        return a + b + c;
    }
}
public class Main {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        System.out.println(calculator.add(10, 20));
        System.out.println(calculator.add(10, 20, 30));
    }

Output

30
60

Here, the compiler selects the appropriate method during compilation.

Runtime Polymorphism

Runtime polymorphism is also called:

  • Dynamic Polymorphism
  • Late Binding

It is achieved using Method Overriding.

The JVM determines which method to execute during runtime based on the actual object.

Example:


class Animal {
    void sound() {
        System.out.println("Animal Sound");
    }
}
class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog Barks");
    }
}
public class Main {
    public static void main(String[] args) {
        Animal animal = new Dog();
        animal.sound();
    }
}

Output

Dog Barks

Although the reference type is Animal, the actual object is Dog, so the overridden method is executed.

Method Overloading vs Method Overriding

Method Overloading Method Overriding
Same class Parent and child classes
Same method name Same method name
Different parameter list Same parameter list
Compile-time polymorphism Runtime polymorphism
Compiler decides JVM decides
Inheritance not required Inheritance required

How Runtime Polymorphism Works


Animal animal = new Dog();
        │
        ▼
Reference Type = Animal
        │
        ▼
Actual Object = Dog
        │
        ▼
Dog's sound() Executes

The method executed depends on the object’s actual type rather than the reference type.

Real-Life Example

Suppose you are developing a Payment System.


class Payment {
    void pay() {
        System.out.println("Processing Payment");
    }
}
class CreditCard extends Payment {
    @Override
    void pay() {
        System.out.println("Paid using Credit Card");
    }
}
class UPI extends Payment {
    @Override
    void pay() {
        System.out.println("Paid using UPI");
    }
}
public class Main {
    pulic static void main(String[] args) {
        Payment payment;
        payment = new CreditCard();
        payment.pay();
        payment = new UPI();
        payment.pay();
    }
}

Output

Paid using Credit Card
Paid using UPI

The same reference variable executes different implementations depending on the object.

Advantages of Polymorphism

Polymorphism provides several benefits:

  • Promotes code reusability.
  • Makes applications flexible.
  • Simplifies maintenance.
  • Supports extensibility.
  • Reduces duplicate code.
  • Encourages loose coupling.
  • Improves readability.
  • Forms the foundation of framework design.

Best Practices

Follow these best practices when using polymorphism:

📖
Best Practices:
  • Program to interfaces or parent classes whenever possible.
  • Use method overriding only when behavior genuinely differs.
  • Use the @Override annotation for overridden methods.
  • Keep overridden methods consistent with the parent class contract.
  • Avoid unnecessary method overloading that makes code confusing.
  • Prefer meaningful method names.
  • Keep inheritance hierarchies simple and logical.

Common Mistakes

Beginners often make these mistakes:

  • Confusing overloading with overriding.
  • Assuming overloaded methods demonstrate runtime polymorphism.
  • Forgetting that runtime polymorphism requires inheritance.
  • Changing method parameters while trying to override a method.
  • Ignoring the @Override annotation.
  • Expecting static methods to participate in runtime polymorphism.
  • Creating deep inheritance hierarchies without a clear design.

Conclusion

Polymorphism is one of the most powerful concepts in Java because it allows a single interface or method name to represent multiple behaviors. By supporting both compile-time polymorphism through method overloading and runtime polymorphism through method overriding, Java enables developers to write flexible, reusable, and maintainable applications.

Java Polymorphism – Interview Questions

Q 1: What is polymorphism in Java?
Ans: Polymorphism allows methods to behave differently.
Q 2: How many types of polymorphism exist?
Ans: Compile-time and runtime polymorphism.
Q 3: What is compile-time polymorphism?
Ans: Achieved using method overloading.
Q 4: What is runtime polymorphism?
Ans: Achieved using method overriding.
Q 5: Why is polymorphism useful?
Ans: It improves flexibility and code reuse.

Java Polymorphism – Objective Questions (MCQs)

Q1. What is polymorphism in Java?






Q2. Which of the following types of polymorphism does Java support?






Q3. Method overloading is an example of:






Q4. Method overriding is an example of:






Q5. Which keyword is used to call the parent class method when it is overridden in a subclass?






Related Java Tutorials