OOP · The Four Pillars
Polymorphism
Less Code Duplication – Avoid writing the same code repeatedly.
- Less Code Duplication – Avoid writing the same code repeatedly.
- Easy Maintenance – Update common code in one place.
- Improved Readability – Keeps code organized and easier to understand.
- Extensibility – Easily add new child classes with additional features.
- Supports Polymorphism – Enables method overriding for flexible behavior.
- Real-World Modeling – Represents IS-A relationships effectively.
Real-World Problem
Imagine you are developing an Online Payment System. The application supports multiple payment methods:
- 💳 Credit Card
- 📱 UPI
- 👛 Wallet
Without polymorphism, you have to check the payment type every time before processing the payment.

Problems Without Polymorphism
- Large if-else blocks.
- Difficult to maintain.
- Hard to add new payment methods.
- Violates the Open/Closed Principle.
Solution: Polymorphism Instead of checking the payment type using multiple conditions, create a common Payment type and let each payment method implement its own behavior. The application simply calls:
Method Overloading Method Overloading is a feature where multiple methods have the same name but differ in the number, type, or order of parameters. It is resolved by the compiler during compilation.
Key Points


- Same method name.
- Different parameter list.
- Occurs within the same class.
- Example of Compile-Time Polymorphism.
Method Overriding Method Overriding allows a child class to provide its own implementation of a method already defined in the parent class. The method signature must remain the same.
Key Points

- Same method signature.
- Parent and child classes required.
- Uses inheritance.
- Example of Runtime Polymorphism.
Compile-Time Polymorphism Compile-Time Polymorphism is achieved through Method Overloading. The compiler determines which method to invoke based on the method signature during compilation.
Key Points
- Achieved using Method Overloading.
- Decision made at compile time.

- Faster execution.
- Also called Static Binding.
Runtime Polymorphism Runtime Polymorphism is achieved through Method Overriding. The JVM decides which overridden method to execute based on the actual object at runtime.
Key Points
- Achieved using Method Overriding.
- Decision made at runtime.
- Supports dynamic behavior.
- Also called Dynamic Binding.

Dynamic Method Dispatch Dynamic Method Dispatch is the process by which the JVM determines which overridden method to call at runtime based on the actual object, not the reference type.


One reference type, many runtime forms — the actual object decides which method runs.
class Shape {
double area() { return 0; }
}
class Circle extends Shape {
double r;
Circle(double r) { this.r = r; }
double area() { return Math.PI * r * r; } // overrides
}
class Square extends Shape {
double s;
Square(double s) { this.s = s; }
double area() { return s * s; }
}
public class Main {
public static void main(String[] args) {
Shape[] shapes = { new Circle(2), new Square(3) };
for (Shape shape : shapes)
System.out.println(shape.area()); // dynamic dispatch picks the right area()
}
}