ProLesson 4.6Intermediate3 min readUpdated 30 October 2026
Polymorphism: overloading vs overriding
One method call, different behaviour. Learn overriding and dynamic dispatch, how it differs from overloading, and the rules interviewers test.
Polymorphism means "many forms": the same method call can do different things depending on the actual object. It's what lets you write code once against a general type and have it work for every specific type.
01Overriding: a child replaces a parent's method
Payments.java
class Payment {
double fee(double amount) {
return 0;
}
String name() {
return "Generic payment";
}
}
class CardPayment extends Payment {
@Override
double fee(double amount) {
return amount * 0.02; // 2% card fee
}
@Override
String name() {
return "Card";
}
}
class UpiPayment extends Payment {
@Override
String name() {
return "UPI"; // fee() not overridden: uses the parent's 0
}
}
public class Payments {
public static void main(String[] args) {
Payment[] payments = { new CardPayment(), new UpiPayment(), new Payment() };
for (Payment p : payments) {
System.out.println(p.name() + " fee on 1000: " + p.fee(1000));
}
}
}Card fee on 1000: 20.0 UPI fee on 1000: 0.0 Generic payment fee on 1000: 0.0
The loop only knows about Payment, yet each object runs its own version of name() and fee(). That's polymorphism.
Loading the rest of the lesson…