In this post, I’ll explain what polymorphism means in Java, and how I understood the it. I’ll also share some examples that helped me understand, remember and use it better.
Polymorphism is actually a pretty neat concept that helps you write cleaner, smarter, and more flexible code.
In this blog, I to broken it down into two flavour’s—Static and Dynamic—and use some everyday examples to make it all click.
🔷 Static Polymorphism (Method Overloading)
Let’s start with static polymorphism. This happens at compile time, which means the Java compiler decides which method to run before the program actually runs.
This is usually done through something called method overloading. That just means you can have multiple methods with the same name, but with different parameters (like number of parameters or types of parameters).
🍹 My Easy Example: Ordering Juice
Let’s say we have a small juice shop. I created a class that lets us order juice in different ways:
class JuiceShop {
void orderJuice() {
System.out.println("Ordered a default orange juice.");
}
void orderJuice(String type) {
System.out.println("Ordered a " + type + " juice.");
}
void orderJuice(String type, int quantity) {
System.out.println("Ordered " + quantity + " " + type + " juice(s).");
}
}
Now when we use this in the main method:
public class Main {
public static void main(String[] args) {
JuiceShop shop = new JuiceShop();
shop.orderJuice(); // No parameters
shop.orderJuice("Mango"); // One parameter
shop.orderJuice("Apple", 3); // Two parameters
}
}
The same method name orderJuice
is used in three different ways. The compiler knows which one to use by looking at the parameters.
✅ What I learned:
Static polymorphism is decided before the program runs
Same method name, different parameters
No inheritance is needed
Very useful when you want to perform similar actions with different inputs
🔶 Dynamic Polymorphism (Method Overriding)
Dynamic polymorphism is a little different. This happens at runtime—which means Java decides which method to run while the program is running. This is done using method overriding.
For this to work, you need to use inheritance. That means one class extends another and overrides one of its methods.
💳 My Simple Example: Making a Payment
Let’s say we have a basic payment system. People can pay in different ways: cash, credit card, or UPI. The action is the same (pay), but how it happens depends on the payment type.
class Payment {
void pay() {
System.out.println("Paying with cash.");
}
}
class CreditCard extends Payment {
@Override
void pay() {
System.out.println("Paying with credit card.");
}
}
class UPI extends Payment {
@Override
void pay() {
System.out.println("Paying via UPI.");
}
}
In the main method:
public class Main {
public static void main(String[] args) {
Payment payment;
payment = new CreditCard();
payment.pay(); // Output: Paying with credit card.
payment = new UPI();
payment.pay(); // Output: Paying via UPI.
}
}
Even though the reference type is Payment
, the actual object type (CreditCard
or UPI
) decides which pay()
method will be called.
✅ What I understood:
Dynamic polymorphism is decided during program execution
It uses method overriding
It needs inheritance
Very useful when different classes should behave differently but share the same method name
📊 Quick Comparison Table
Here’s a quick summary that helped me remember the difference:
FeatureStatic PolymorphismDynamic PolymorphismHappens when?During compile timeDuring runtimeHow it works?Method OverloadingMethod OverridingInheritance needed?❌ No✅ YesFlexibility🚫 Less flexible✅ More flexibleSpeed✅ Faster🚫 A bit slower
💬 Final Thoughts
At first, I thought polymorphism was hard to understand. But once I started thinking about simple real-life examples (like juice shops and payment systems), it made much more sense.
Here’s what helped me:
Use method overloading (static) when the action is the same but inputs are different.
Use method overriding (dynamic) when you want different behaviors depending on the object type.
If you’re new to Java or still learning OOP, don’t worry—it’s normal to feel confused at first. Just keep practicing and try writing your own examples.
Let me know if you'd like a version of this post with visuals or step-by-step code walkthroughs!