FirstHack 2026 — a 36-hour online hackathon built for first-year students.•₹1,00,000 prize pool•22 Oct – 4 Nov•Open to every branch and every stream•Certificate for every team that submits•3 rounds•FirstHack 2026 — a 36-hour online hackathon built for first-year students.•₹1,00,000 prize pool•22 Oct – 4 Nov•Open to every branch and every stream•Certificate for every team that submits•3 rounds•
One loop that handles types it has never heard of, and the difference between overloading and overriding.
12 min read·Lesson 4 of 6·Pro
The problem, in code you have probably written
Java
for (Object o : things) {
if (o instanceof Circle) {
total += ((Circle) o).circleArea();
} else if (o instanceof Rect) {
total += ((Rect) o).rectArea();
} else if (o instanceof Triangle) {
total += ((Triangle) o).triangleArea();
}
}
Every new shape means editing this loop. If the same chain appears in five files, that is five edits, and you will forget one. This is the shape of code that polymorphism deletes.
The polymorphic version
Java
abstract class Shape {
abstract double area();
The rest of this lesson is Pro
The free lessons of Object-Oriented Programming finish what
they start — read those first if you have not. This one goes further, and it
is part of the paid half.
A pass opens the paid lessons of every course, the mock test papers
and the company-wise series. It ends on its own date; nothing renews by itself.