What is polymorphism?
25 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Polymorphism means one action, many forms. As What is OOP? (opens in a new tab) put it, the same Share button sends a photo on WhatsApp, attaches it in Gmail or uploads it to Drive. In Java, the same method call can run different code for different objects.
One action, many forms
Polymorphism is the ability of one method call or one method name to take many forms: the same call does different work depending on the object it runs on, or on the arguments it gets. The word means "many forms".
At a shop counter the cashier says "Pay Rs. 500". Aarav pays by UPI, Priya taps a card and Rohan hands over cash. One request, three ways of doing it. In Java, pay(500) works the same way.
Example 1 · Three objects, one call
class Payment { void pay(int amount) { System.out.println("Paying Rs. " + amount); }}
class UpiPayment extends Payment { void pay(int amount) { System.out.println("UPI: Rs. " + amount + " sent from phone"); }}
class CardPayment extends Payment { void pay(int amount) { System.out.println("Card: Rs. " + amount + " charged"); }}
class CashPayment extends Payment {}
public class PayDemo { public static void main(String[] args) { new UpiPayment().pay(500); new CardPayment().pay(500); new CashPayment().pay(500); }}UPI: Rs. 500 sent from phone Card: Rs. 500 charged Paying Rs. 500
What this code does
- The three calls are the same,
pay(500), but each prints a different line. UpiPaymentandCardPaymentwrite their ownpaywith the same name and parameter, so their version runs.CashPaymentwrites nothing, so it uses thepayit inherited fromPayment.
Each class decides how to pay; the caller only says pay. Writing your own version of an inherited method like this is called overriding, and Method overriding (opens in a new tab) covers its rules.
Compile-time polymorphism
Compile-time polymorphism, also called static polymorphism, is when the compiler decides which method a call runs, before the program starts. Java gets it through method overloading: one method name with different parameter lists.
You met this in Method overloading (opens in a new tab). Method overloading is several methods with one name but different parameters. The compiler reads each call's arguments and fixes the version before the program runs.
Example 2 · Overloading: the compiler picks by arguments
public class CanteenBill { static int total(int price, int qty) { return price * qty; }
static double total(double price, int qty) { return price * qty; }
static int total(int price, int qty, int discount) { return price * qty - discount; }
public static void main(String[] args) { System.out.println(total(20, 3)); System.out.println(total(12.5, 4)); System.out.println(total(40, 5, 25)); }}60 50.0 175
What this code does
- Three methods share the name
total; their parameter lists differ in type or count. total(12.5, 4)has adoublefirst argument, so the compiler picks thedoubleversion, which returns50.0.- Every choice is made by
javacfrom the arguments alone. No objects and no inheritance are involved.
Compile time is when javac checks your code and turns it into bytecode. Run time is when the JVM actually runs it. For overloading, the choice is already made at compile time; nothing about it is left for the JVM.
Runtime polymorphism
Runtime polymorphism, also called dynamic polymorphism, is when the JVM decides which method a call runs while the program is running, by looking at the actual object. Java gets it through method overriding in child classes.
A checkout method takes any payment and calls p.pay(amount). It cannot know which kind of payment it will get. The object that arrives decides which pay runs.
Example 3 · Overriding: the object decides at run time
class Payment { void pay(int amount) { System.out.println("Paying Rs. " + amount); }}
class UpiPayment extends Payment { void pay(int amount) { System.out.println("UPI: Rs. " + amount + " sent from phone"); }}
class CardPayment extends Payment { void pay(int amount) { System.out.println("Card: Rs. " + amount + " charged"); }}
public class CheckoutDemo { static void checkout(Payment p, int amount) { System.out.println("Order total: Rs. " + amount); p.pay(amount); }
public static void main(String[] args) { checkout(new UpiPayment(), 450); checkout(new CardPayment(), 1200); }}Order total: Rs. 450 UPI: Rs. 450 sent from phone Order total: Rs. 1200 Card: Rs. 1200 charged
What this code does
- The parameter type is
Payment, socheckoutaccepts any child object. - The line
p.pay(amount)is written once, yet it runs the UPI version first and the card version second. - The compiler cannot know which object will arrive, so the JVM decides while the program runs.
Runtime polymorphism needs three things, and each has its own lesson in this module:
- a parent class and child classes, as in Inheritance (opens in a new tab)
- a child method with the same name and parameters: Method overriding (opens in a new tab)
- a parent-type variable, parameter or array that holds a child object: Upcasting (opens in a new tab)
Why polymorphism helps
Without polymorphism, one method checks the payment type itself with an if-else chain on a String. Every new payment type means finding and editing that chain, in every place it appears.
Example 4 · Without polymorphism: an if-else chain
public class IfElseCheckout { static void pay(String type, int amount) { if (type.equals("UPI")) { System.out.println("UPI: Rs. " + amount + " sent from phone"); } else if (type.equals("CARD")) { System.out.println("Card: Rs. " + amount + " charged"); } else { System.out.println("Paying Rs. " + amount); } }
public static void main(String[] args) { pay("UPI", 450); pay("CARD", 1200); pay("WALLET", 300); }}UPI: Rs. 450 sent from phone Card: Rs. 1200 charged Paying Rs. 300
What this code does
- One method holds the logic for every payment type, chosen by a
String. - The new type
"WALLET"silently falls intoelseand prints the wrong line. To support it, someone must find and edit this chain.
Example 5 · With polymorphism: a wallet is one new class
class Payment { void pay(int amount) { System.out.println("Paying Rs. " + amount); }}
class UpiPayment extends Payment { void pay(int amount) { System.out.println("UPI: Rs. " + amount + " sent from phone"); }}
class WalletPayment extends Payment { void pay(int amount) { System.out.println("Wallet: Rs. " + amount + " deducted"); }}
public class WalletDemo { static void checkout(Payment p, int amount) { p.pay(amount); }
public static void main(String[] args) { checkout(new UpiPayment(), 450); checkout(new WalletPayment(), 300); }}UPI: Rs. 450 sent from phone Wallet: Rs. 300 deducted
What this code does
WalletPaymentis the only new code.checkoutis exactly as before.- Each class carries its own way of paying, so there is no chain of
ifchecks to keep up to date.
With polymorphism, each class knows how to pay, and the checkout code only calls pay. A new way of paying is one new child class; the code that calls pay does not change at all.
Next step
Check your understanding
11 quick questions