OOP

Polymorphism

Polymorphism

Polymorphism means "many forms". It is the ability of one method, object, or interface to behave differently in different situations.

In simple words:

Polymorphism = One name, many behaviors

Real-Life Example: Person

A single person can play different roles:

text
1Same person
2 ├── At home → Son / Daughter
3 ├── At college → Student
4 └── At work → Employee

The person is the same, but the behavior changes with the situation. In the same way, one method name can behave differently depending on the context.

Types of Polymorphism in Java

TypeAlso CalledResolved AtAchieved Using
Compile-timeStatic binding / Early bindingCompile timeMethod Overloading
RuntimeDynamic binding / Late bindingRuntimeMethod Overriding
text
1Polymorphism
2 │
3 ├── Compile-time Polymorphism → Method Overloading
4 │
5 └── Runtime Polymorphism → Method Overriding

1. Compile-time Polymorphism (Method Overloading)

Multiple methods have the same name but different parameters (number, type, or order) in the same class. The compiler decides which one to call.

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;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(10, 20)); // 30
System.out.println(calc.add(10, 20, 30)); // 60
System.out.println(calc.add(2.5, 3.5)); // 6.0
}
}

Rules of Overloading

RuleAllowed?
Different number of parametersYes
Different types of parametersYes
Different order of parameter typesYes
Only different return typeNo
Only different parameter namesNo
class Test {
int show(int a) { return a; }
double show(int a) { return a; } // Compile-time error (only return type differs)
}

2. Runtime Polymorphism (Method Overriding)

A child class provides its own version of a method already defined in the parent class. The method that runs is decided at runtime, based on the actual object, not the reference type.

class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal a; // parent reference
a = new Dog();
a.sound(); // Dog barks
a = new Cat();
a.sound(); // Cat meows
}
}
// Output:
// Dog barks
// Cat meows

This is called upcasting: a parent reference holding a child object.

text
1Animal a = new Dog();
2 │ │
3 │ └── Actual object (decides which method runs)
4 └── Reference type

Rules of Overriding

RuleExplanation
Same method name and parametersMust match the parent method
Requires inheritanceChild class must extend the parent
Access level cannot be reducedpublic cannot become private
Return type must be same (or covariant)Child can return a subtype
static, final, private methodsCannot be overridden

Real-World Example: Payment System

class Payment {
void pay(double amount) {
System.out.println("Paying " + amount);
}
}
class UpiPayment extends Payment {
@Override
void pay(double amount) {
System.out.println("Paid ₹" + amount + " using UPI");
}
}
class CardPayment extends Payment {
@Override
void pay(double amount) {
System.out.println("Paid ₹" + amount + " using Card");
}
}
public class Main {
static void checkout(Payment payment, double amount) {
payment.pay(amount); // behavior depends on the actual object
}
public static void main(String[] args) {
checkout(new UpiPayment(), 500);
checkout(new CardPayment(), 1200);
}
}
// Output:
// Paid ₹500.0 using UPI
// Paid ₹1200.0 using Card

The checkout() method works with any payment type. A new payment type can be added later without changing checkout().

Overloading vs Overriding

FeatureOverloadingOverriding
PolymorphismCompile-timeRuntime
LocationSame classParent and child class
ParametersMust be differentMust be the same
Return typeCan differSame or covariant
InheritanceNot requiredRequired
BindingStatic (early)Dynamic (late)

Key Benefits

BenefitExplanation
FlexibilityOne interface, many implementations
ExtensibilityAdd new classes without changing existing code
ReadabilitySame method name for similar actions
Loose couplingCode depends on the parent type, not specific child

Interview Definition

Polymorphism is an OOP principle that allows the same method or interface to exhibit different behaviors, achieved through method overloading at compile time and method overriding at runtime.

Remember

Overloading = same name, different parameters (compile time). Overriding = same signature, different class (runtime).

Next TopicAbstraction