Interview Questions

Understanding Java Polymorphism: Overloading vs. Overriding Explained Simply

Author: Interview Questions Posted 4 days ago 21 min read

At its core, polymorphism means "one interface, many implementations." You write code against a general type (like a parent class or interface), and Java decides which specific behavior to run.


1. Runtime Polymorphism (Method Overriding) — The Real Superpower

Suppose you have a PaymentMethod type, and different payment options (CreditCard, PayPal, BankTransfer) each process payments differently.

Without polymorphism, you might write:

if (type.equals("card")) {
    // charge card
} else if (type.equals("paypal")) {
    // charge paypal
}

With method overriding, a subclass provides its own version of a method already defined in its parent class or interface:

interface PaymentMethod {
    void pay(double amount);
}

class CreditCard implements PaymentMethod {
    @Override
    public void pay(double amount) {
        System.out.println("Charging $" + amount + " to Credit Card");
    }
}

class PayPal implements PaymentMethod {
    @Override
    public void pay(double amount) {
        System.out.println("Charging $" + amount + " via PayPal");
    }
}

Now look at how a single method handles any of them:

public class Checkout {
    public static void processOrder(PaymentMethod method, double amount) {
        // Variable type is PaymentMethod, actual object is CreditCard or PayPal
        method.pay(amount);
    }

    public static void main(String[] args) {
        processOrder(new CreditCard(), 49.99); // Charging $49.99 to Credit Card
        processOrder(new PayPal(), 49.99);     // Charging $49.99 via PayPal
    }
}

Why is it called runtime polymorphism? Because the compiler only checks that PaymentMethod has a pay(double) method. While the program is actually running (runtime), the JVM looks at the actual object on the heap (CreditCard vs. PayPal) and calls that specific implementation.


2. Compile-Time Polymorphism (Method Overloading) — Same Name, Different Inputs

Method overloading is simpler: multiple methods in the same class share the same name, as long as their parameter lists differ (different number, types, or order of parameters).

class Logger {
    public void log(String message) {
        System.out.println("[INFO] " + message);
    }

    public void log(String message, int errorCode) {
        System.out.println("[ERROR " + errorCode + "] " + message);
    }

    public void log(int count) {
        System.out.println("[COUNT] " + count);
    }
}

Why is it called compile-time polymorphism? Because the Java compiler already knows which method to call before the code even runs, based on the arguments you pass (log("Started") vs. log("Failed", 500)).

(Note: Many Java developers consider method overloading more of a convenience feature than "true" object-oriented polymorphism, whereas overriding with parent/interface references is the main OOP mechanism interviewers care about.)


Quick Comparison

Feature Method Overloading Method Overriding
Where it happens Same class Subclass (child) overriding superclass/interface (parent)
Method signature Name same; parameters must differ Name and parameters must match
Resolved when Compile time Runtime (based on actual object type)
Main benefit Convenient variations of an action Swappable behavior behind a common type

A Mental Model That Sticks

Think of a TV remote:

  • Pressing Volume Up works whether the TV is a Sony, LG, or Samsung.
  • Your hand uses the same action (remote.volumeUp()), and each TV executes its own internal logic at runtime.

In interview terms: Overriding lets you program to an interface/superclass and swap implementations cleanly without changing caller code; overloading lets one method name accept different parameter combinations.