👁 Preview — Study, Practice and Revise are open; mock tests and the rest of the syllabus unlock on subscription. Unlock all · ₹4,999
← Back to OOP Concepts
Study mode

Polymorphism

Introduction to Polymorphism

Imagine you have a remote control that can operate different devices: a TV, an air conditioner, and a music system. Even though you use the same remote, pressing the "Power" button turns on different devices in different ways. This is a simple example of polymorphism - the ability of something to take many forms.

In Object-Oriented Programming (OOP), polymorphism allows objects of different classes to be treated as objects of a common superclass, while still behaving differently based on their actual class. This flexibility is crucial for writing maintainable and scalable software.

Polymorphism works closely with other OOP concepts like inheritance (where classes inherit properties and behaviors from other classes) and abstraction (hiding complex details and showing only necessary features). Together, they enable programmers to design systems that are easier to extend and modify.

Definition of Polymorphism

Formally, polymorphism means "many forms." In programming, it refers to the ability of a single interface or method to represent different underlying forms (data types or classes).

There are two main types of polymorphism in OOP:

Comparison of Compile-time and Run-time Polymorphism
Aspect Compile-time Polymorphism Run-time Polymorphism
Also called Method Overloading Method Overriding
Binding time At compile time (before program runs) At run time (during program execution)
How it works Multiple methods with same name but different parameters Subclass provides specific implementation of superclass method
Inheritance required? No Yes
Example Calculator class with multiple add() methods Animal class with subclasses Dog and Cat overriding sound()

Method Overloading (Compile-time Polymorphism)

Method overloading means having multiple methods in the same class with the same name but different parameter lists (different number, type, or order of parameters). The compiler decides which method to call based on the arguments passed.

Think of it like a Swiss Army knife: one tool handle, but many different blades and functions depending on what you need.

graph TD    A[Call method foo()] --> B{Check parameters}    B -->|1 int| C[Call foo(int)]    B -->|2 ints| D[Call foo(int, int)]    B -->|1 string| E[Call foo(string)]

Here, the compiler looks at the method call and matches it to the correct method signature.

Method Overriding (Run-time Polymorphism)

Method overriding occurs when a subclass provides its own version of a method already defined in its superclass. At run time, the program decides which method to invoke based on the actual object type, not the reference type.

This is like a universal remote that sends the "Power" command, but the device receiving it decides how to respond.

sequenceDiagram    participant Caller    participant Superclass    participant Subclass    Caller->>Superclass: call sound()    alt Object is superclass        Superclass-->>Caller: Superclass sound()    else Object is subclass        Subclass-->>Caller: Subclass overridden sound()    end

This dynamic method dispatch is achieved through virtual functions or similar mechanisms in languages like Java and C++.

Worked Examples

Example 1: Method Overloading Example Easy
Consider a Calculator class that can add numbers. Implement multiple add() methods to add two integers, three integers, and two doubles.

Step 1: Define the Calculator class with three add() methods:

class Calculator {    int add(int a, int b) {        return a + b;    }    int add(int a, int b, int c) {        return a + b + c;    }    double add(double a, double b) {        return a + b;    }}    

Step 2: Use the methods:

Calculator calc = new Calculator();System.out.println(calc.add(5, 10));        // Calls add(int, int)System.out.println(calc.add(1, 2, 3));      // Calls add(int, int, int)System.out.println(calc.add(2.5, 3.5));     // Calls add(double, double)    

Step 3: The compiler selects the correct method based on parameters.

Answer: Output will be:

15
6
6.0

Example 2: Method Overriding Example Medium
Create a base class Animal with a method sound(). Create subclasses Dog and Cat that override sound() to print their respective sounds. Demonstrate run-time polymorphism.

Step 1: Define the base class and subclasses:

class Animal {    void sound() {        System.out.println("Some generic animal sound");    }}class Dog extends Animal {    @Override    void sound() {        System.out.println("Bark");    }}class Cat extends Animal {    @Override    void sound() {        System.out.println("Meow");    }}    

Step 2: Create Animal references to Dog and Cat objects:

Animal a1 = new Dog();Animal a2 = new Cat();a1.sound();  // Calls Dog's sound()a2.sound();  // Calls Cat's sound()    

Step 3: At run time, the overridden methods in Dog and Cat are called, not the base class method.

Answer: Output will be:

Bark
Meow

Example 3: Polymorphism in Action with Shapes Medium
Design a Shape superclass with a draw() method. Create subclasses Circle, Rectangle, and Triangle that override draw(). Store different shapes in a collection and call draw() on each to demonstrate polymorphism.

Step 1: Define the Shape class and subclasses:

class Shape {    void draw() {        System.out.println("Drawing a shape");    }}class Circle extends Shape {    @Override    void draw() {        System.out.println("Drawing a circle");    }}class Rectangle extends Shape {    @Override    void draw() {        System.out.println("Drawing a rectangle");    }}class Triangle extends Shape {    @Override    void draw() {        System.out.println("Drawing a triangle");    }}    

Step 2: Store different shapes in an array and call draw():

Shape[] shapes = {new Circle(), new Rectangle(), new Triangle()};for (Shape s : shapes) {    s.draw();}    

Step 3: Each object's draw() method is called according to its actual type.

Answer: Output will be:

Drawing a circle
Drawing a rectangle
Drawing a triangle

Example 4: Exam-style Question on Polymorphism Hard
Given the following code snippet, identify the type of polymorphism and predict the output:
class Parent {    void show() {        System.out.println("Parent show");    }}class Child extends Parent {    void show() {        System.out.println("Child show");    }}public class Test {    public static void main(String[] args) {        Parent p = new Child();        p.show();    }}    

Step 1: Identify polymorphism type.

This is method overriding (run-time polymorphism) because the Child class provides its own implementation of show(), and the Parent reference points to a Child object.

Step 2: Predict output.

At run time, the Child's show() method is called.

Answer: Output will be:
Child show

Example 5: Polymorphism with Interfaces Hard
Define an interface Drawable with a method draw(). Implement this interface in classes Line, Circle, and Square. Demonstrate polymorphism by calling draw() on an array of Drawable objects.

Step 1: Define the interface and implementing classes:

interface Drawable {    void draw();}class Line implements Drawable {    public void draw() {        System.out.println("Drawing a line");    }}class Circle implements Drawable {    public void draw() {        System.out.println("Drawing a circle");    }}class Square implements Drawable {    public void draw() {        System.out.println("Drawing a square");    }}    

Step 2: Create an array of Drawable and call draw():

Drawable[] drawables = {new Line(), new Circle(), new Square()};for (Drawable d : drawables) {    d.draw();}    

Step 3: Each object's draw() method is called according to its class.

Answer: Output will be:

Drawing a line
Drawing a circle
Drawing a square

Tips & Tricks

Tip: Remember: Overloading is compile-time, Overriding is run-time.

When to use: When distinguishing between types of polymorphism in exam questions.

Tip: Look for method signatures (parameter lists) to identify overloading.

When to use: When analyzing code snippets with multiple methods of the same name.

Tip: Check the class hierarchy to spot overriding - overridden methods appear in subclasses.

When to use: When determining if a method is overridden in a subclass.

Tip: Use real-world analogies like "Shapes drawing themselves" to remember polymorphism.

When to use: For conceptual clarity and memory retention.

Tip: Practice predicting output of polymorphic code to improve exam performance.

When to use: Before attempting competitive exam questions on OOP.

Common Mistakes to Avoid

❌ Confusing method overloading with method overriding
✓ Understand that overloading involves same method name with different parameters in the same class, overriding involves redefining a superclass method in subclass
Why: Both involve methods with the same name, causing confusion
❌ Ignoring access modifiers when overriding methods
✓ Ensure overridden methods have the same or more accessible modifiers
Why: Access modifiers affect method visibility and can cause compilation errors
❌ Assuming polymorphism works without inheritance or interface implementation
✓ Polymorphism requires inheritance or interface implementation to work
Why: Polymorphism depends on class hierarchy or interface contracts
❌ Expecting compile-time polymorphism to work with different return types only
✓ Overloading requires different parameter lists; return type alone cannot distinguish overloaded methods
Why: Compiler uses parameters, not return type, to resolve overloaded methods
❌ Not using @Override annotation leading to silent errors
✓ Use @Override annotation to ensure correct overriding
Why: Prevents mistakes like incorrect method signatures
Key Concept

Polymorphism in OOP

Ability of objects to take many forms, enabling flexible and maintainable code.

Key Concept

Compile-time Polymorphism

Method overloading: same method name, different parameters, resolved at compile time.

Key Concept

Run-time Polymorphism

Method overriding: subclass provides specific method implementation, resolved at run time.

Key Concept

Benefits of Polymorphism

Simplifies code, enhances flexibility, supports code reuse, and eases maintenance.

Curated videos per subtopic
Top YouTube explainers, AI-ranked for your exam and language. Unlocks with subscription.
Unlock

Try Practice next.

Progress tracking is paywalled — subscribe to mark subtopics as understood and save your streak.

Go to practice →
Ask a doubt
Polymorphism · 10 free messages
Ask me anything about this subtopic. You have 10 free messages this session — chat history isn't saved in preview.