Java Polymorphism


Polymorphism is another key concept of Object-Oriented Programming in Java. The word “polymorphism” comes from two Greek words: poly meaning “many,” and morph meaning “forms.” So, polymorphism literally means “many forms.”

In Java, polymorphism allows one object to behave in multiple ways, depending on the context. It enables a single method name or interface to represent different underlying forms (data types or classes).

What Is Polymorphism in Java?

Polymorphism means the same method or object can perform differently depending on which class it belongs to. This makes code more flexible, maintainable, and easier to extend.

For example, consider a method makeSound() that behaves differently for Dog and Cat. Both share the same method name, but their output changes based on the actual object.

Types of Polymorphism in Java

Java supports two types of polymorphism:

  1. Compile-time Polymorphism (Static Binding)

  2. Runtime Polymorphism (Dynamic Binding)

Let’s understand both in detail.

1. Compile-time Polymorphism (Method Overloading)

Compile-time polymorphism happens when multiple methods have the same name but different parameters. This is also known as method overloading.

The compiler determines which method to call during compilation based on the number or type of parameters.

Example: Method Overloading

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

    double add(double a, double 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 calc = new Calculator();
        System.out.println(calc.add(5, 10));        // calls first method
        System.out.println(calc.add(4.5, 3.2));     // calls second method
        System.out.println(calc.add(1, 2, 3));      // calls third method
    }
}

Output:

15  
7.7  
6

Here, the method add() performs different operations depending on the number and type of arguments.

2. Runtime Polymorphism (Method Overriding)

Runtime polymorphism occurs when a subclass provides a specific implementation of a method that is already defined in its parent class.

This is known as method overriding. The method to be called is determined at runtime — not at compile-time.

Example: Method Overriding

class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

class Cat extends Animal {
    void sound() {
        System.out.println("Cat meows");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal a1 = new Dog();  // reference of parent, object of child
        Animal a2 = new Cat();

        a1.sound(); // Dog's version
        a2.sound(); // Cat's version
    }
}

Output:

Dog barks  
Cat meows

Here, even though both objects are referenced as Animal, the actual method executed depends on the object type (Dog or Cat). This is runtime polymorphism.

Real-life Example of Polymorphism

Imagine you have a class Payment with a method pay(). Subclasses like CreditCardPayment and UPIPayment override this method. The same call p.pay() performs different actions based on the payment type.

class Payment {
    void pay() {
        System.out.println("Processing payment...");
    }
}

class CreditCardPayment extends Payment {
    void pay() {
        System.out.println("Payment done using Credit Card");
    }
}

class UPIPayment extends Payment {
    void pay() {
        System.out.println("Payment done using UPI");
    }
}

public class Main {
    public static void main(String[] args) {
        Payment p;

        p = new CreditCardPayment();
        p.pay();

        p = new UPIPayment();
        p.pay();
    }
}

Output:

Payment done using Credit Card  
Payment done using UPI

This approach allows new payment methods to be added later without changing the existing code.

Rules for Method Overriding

  1. The method name and parameters must be exactly the same as in the parent class.

  2. The return type must be the same or a subtype (known as covariant return type).

  3. The overridden method cannot have a stricter access level than the parent method.

  4. final, static, and private methods cannot be overridden.

Using the super Keyword with Overriding

The super keyword is used to call the parent class method from the overridden method.

Example:

class Parent {
    void show() {
        System.out.println("Parent class method");
    }
}

class Child extends Parent {
    void show() {
        super.show(); // calls parent version
        System.out.println("Child class method");
    }
}

public class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.show();
    }
}

Output:

Parent class method  
Child class method

Advantages of Polymorphism

  1. Code Reusability – Same interface can be used for different implementations.

  2. Extensibility – New subclasses can be added without modifying existing code.

  3. Maintainability – Reduces duplication and makes the code cleaner.

  4. Dynamic Behavior – The method that executes depends on the object type at runtime.

Example: Polymorphism in Real Systems

Suppose a system has different types of users — Admin, Teacher, and Student. Each one logs in differently. You can achieve this using polymorphism:

class User {
    void login() {
        System.out.println("User login");
    }
}

class Admin extends User {
    void login() {
        System.out.println("Admin login with admin credentials");
    }
}

class Teacher extends User {
    void login() {
        System.out.println("Teacher login with staff ID");
    }
}

class Student extends User {
    void login() {
        System.out.println("Student login with roll number");
    }
}

public class Main {
    public static void main(String[] args) {
        User u;

        u = new Admin();
        u.login();

        u = new Teacher();
        u.login();

        u = new Student();
        u.login();
    }
}

Output:

Admin login with admin credentials  
Teacher login with staff ID  
Student login with roll number

The same method login() behaves differently for each subclass.

Difference Between Compile-time and Runtime Polymorphism

Feature Compile-time Polymorphism Runtime Polymorphism
Implementation Method Overloading Method Overriding
Decision Time Compile-time Runtime
Binding Type Static Binding Dynamic Binding
Keyword Used Same method name, different parameters Same method name, same parameters
Inheritance Required No Yes

Summary of the Tutorial

Polymorphism in Java allows the same action to behave differently based on the object.

  • Compile-time polymorphism uses method overloading.

  • Runtime polymorphism uses method overriding.

  • The super keyword can call the parent version of an overridden method.

  • It improves flexibility, scalability, and reusability of code.

Polymorphism is the real power of OOP — it lets your code handle new situations without modification.


Practice Questions

1. Create a class Vehicle with a method startEngine(). Inherit it in Car and Bike classes and override the method to display different messages for each vehicle type.

2. Write a program that demonstrates method overloading with a class Display. Create multiple show() methods that accept different types of parameters (int, double, String).

3. Create a base class Shape with a method area(). Inherit it in Rectangle, Circle, and Triangle classes, each overriding the area() method with its own formula.

4. Define a parent class Animal with a method makeSound(). Override it in subclasses Dog, Cat, and Cow. Create an array of Animal references and call makeSound() on each object to demonstrate runtime polymorphism.

5. Create a class Calculator that demonstrates method overloading for an add() method:

  • Two integers

  • Three integers

  • Two double values

6. Write a program where class Employee has a method work(). Create a subclass Manager that overrides work() to print a different message. Use the parent reference to call the overridden method.

7. Create an example using the super keyword to call the parent class method from the overridden method in the child class.

8. Write a program that shows runtime polymorphism using a class Bank with a method getRateOfInterest(). Subclasses SBI, HDFC, and ICICI override the method with different interest rates.

9. Demonstrate polymorphism using a class MediaPlayer with a method play(). Create subclasses AudioPlayer and VideoPlayer that override the play() method to print appropriate messages.

10. Create an abstract class Appliance with an abstract method turnOn(). Inherit it in subclasses Fan and Light and provide their own implementations. Call the method using parent references to show polymorphism.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top