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:
1Same person2 ├── At home → Son / Daughter3 ├── At college → Student4 └── At work → EmployeeThe 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
| Type | Also Called | Resolved At | Achieved Using |
|---|---|---|---|
| Compile-time | Static binding / Early binding | Compile time | Method Overloading |
| Runtime | Dynamic binding / Late binding | Runtime | Method Overriding |
1Polymorphism2 │3 ├── Compile-time Polymorphism → Method Overloading4 │5 └── Runtime Polymorphism → Method Overriding1. 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)); // 30System.out.println(calc.add(10, 20, 30)); // 60System.out.println(calc.add(2.5, 3.5)); // 6.0}}
Rules of Overloading
| Rule | Allowed? |
|---|---|
| Different number of parameters | Yes |
| Different types of parameters | Yes |
| Different order of parameter types | Yes |
| Only different return type | No |
| Only different parameter names | No |
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 {@Overridevoid sound() {System.out.println("Dog barks");}}class Cat extends Animal {@Overridevoid sound() {System.out.println("Cat meows");}}public class Main {public static void main(String[] args) {Animal a; // parent referencea = new Dog();a.sound(); // Dog barksa = new Cat();a.sound(); // Cat meows}}// Output:// Dog barks// Cat meows
This is called upcasting: a parent reference holding a child object.
1Animal a = new Dog();2 │ │3 │ └── Actual object (decides which method runs)4 └── Reference typeRules of Overriding
| Rule | Explanation |
|---|---|
| Same method name and parameters | Must match the parent method |
| Requires inheritance | Child class must extend the parent |
| Access level cannot be reduced | public cannot become private |
| Return type must be same (or covariant) | Child can return a subtype |
static, final, private methods | Cannot be overridden |
Real-World Example: Payment System
class Payment {void pay(double amount) {System.out.println("Paying " + amount);}}class UpiPayment extends Payment {@Overridevoid pay(double amount) {System.out.println("Paid ₹" + amount + " using UPI");}}class CardPayment extends Payment {@Overridevoid 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
| Feature | Overloading | Overriding |
|---|---|---|
| Polymorphism | Compile-time | Runtime |
| Location | Same class | Parent and child class |
| Parameters | Must be different | Must be the same |
| Return type | Can differ | Same or covariant |
| Inheritance | Not required | Required |
| Binding | Static (early) | Dynamic (late) |
Key Benefits
| Benefit | Explanation |
|---|---|
| Flexibility | One interface, many implementations |
| Extensibility | Add new classes without changing existing code |
| Readability | Same method name for similar actions |
| Loose coupling | Code 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).