java
- IntroductionInstallation and SetupJava Syntax and StructureRunning Your First ProgramJava VariablesData Types in JavaOperators in JavaJava Input and OutputControl StatementsLoops in JavaMethods in Java (Functions)Arrays in JavaString Handling in JavaOOPS Concept in JavaClasses and ObjectsConstructor and Constructor Overloadingthis KeywordStatic MembersInheritanceAggregation in JavaMethod OverloadingMethod Overridingsuper KeywordFinal Keyword with Class, Method, and VariablesAccess Modifiers in javaEncapsulation in Java Polymorphism in JavaAbstraction in JavaAbstract ClassesInterfaces in JavaDifference between Abstract Class and Interface Nested and Inner Classes Exception HandlingJava PackagesWrapper Classes and AutoboxingJava Collections FrameworkFile Handling in JavaMultithreadingBasics of Java Memory Management and Garbage CollectionJava JDBC
Polymorphism in Java – Complete Tutorial
Last Updated on: 18th Nov 2025 17:50:58 PM
Welcome to this complete, beginner-friendly tutorial on Polymorphism in Java — one of the most powerful concepts in Object-Oriented Programming!
Polymorphism allows objects of different classes to be treated as objects of a common superclass. The word “Polymorphism” literally means “many forms” — the same entity can behave differently depending on the actual object at runtime.
This tutorial uses a real-world project — a Modern Payment Gateway System (like Paytm, PhonePe, Google Pay) — to show how polymorphism is used daily in production applications.
What is Polymorphism?
The word Polymorphism means: “One name – many forms” In Java, polymorphism allows a single action (method) to behave differently based on the object performing it.
Types of Polymorphism in Java
Java supports 2 types of polymorphism:
-
Compile-Time Polymorphism (Method Overloading)
-
Runtime Polymorphism (Method Overriding)
1. Compile-Time Polymorphism (Method Overloading)
Method Overloading occurs when multiple methods have the same name, but different parameters (different type, number, or order).
Why Used?
-
Increases readability
-
Same action with different inputs
Real-Life Example of Method Overloading (Compile-Time Polymorphism)
Real-Life Scenario: ATM Cash Deposit
At an ATM, you can deposit money in multiple ways:
-
Deposit cash
-
Deposit cheque
-
Deposit using QR/UPI
The action is same → deposit(), but inputs differ.
Java Example:
class ATM {
// Cash Deposit
void deposit(int cashAmount) {
System.out.println("Cash Deposit: ₹" + cashAmount);
}
// Cheque Deposit
void deposit(String chequeNumber, double chequeAmount) {
System.out.println("Cheque Deposit - Cheque No: " + chequeNumber + " Amount: ₹" + chequeAmount);
}
// UPI Deposit
void deposit(String upiId, int amount) {
System.out.println("UPI Deposit from " + upiId + ": ₹" + amount);
}
}
public class Main {
public static void main(String[] args) {
ATM atm = new ATM();
atm.deposit(2000); // Cash
atm.deposit("CHQ12345", 5000.50); // Cheque
atm.deposit("user@upi", 1500); // UPI
}
}
Output:
Cash Deposit: ₹2000
Cheque Deposit - Cheque No: CHQ12345 Amount: ₹5000.5
UPI Deposit from user@upi: ₹1500
👉 Same method name → different input types → different behaviors .This is Method Overloading.
2. Runtime Polymorphism (Method Overriding)
Method Overriding allows a child class to provide its own implementation of a method already present in the parent class.
Why Used?
-
For dynamic behavior
-
To achieve runtime polymorphism
-
Used heavily in frameworks (Spring, Hibernate, OOP design patterns)
Real-Life Example of Method Overriding (Runtime Polymorphism)
Real-Life Scenario: Online Food Delivery App (Zomato/Swiggy)
Every food order has a payment() method, but each payment mode behaves differently:
-
UPI → “Paid using UPI”
-
Card → “Paid using Card”
-
Wallet → “Paid using Wallet”
Same method name → different behavior depending on object.
Java Example:
class Payment {
void pay() {
System.out.println("Payment processing...");
}
}
class UpiPayment extends Payment {
void pay() {
System.out.println("Payment completed using UPI");
}
}
class CardPayment extends Payment {
void pay() {
System.out.println("Payment completed using Debit/Credit Card");
}
}
class WalletPayment extends Payment {
void pay() {
System.out.println("Payment completed using Wallet Balance");
}
}
public class Main {
public static void main(String[] args) {
Payment p;
p = new UpiPayment();
p.pay(); // UPI version
p = new CardPayment();
p.pay(); // Card version
p = new WalletPayment();
p.pay(); // Wallet version
}
}
Output:
Payment completed using UPI
Payment completed using Debit/Credit Card
Payment completed using Wallet Balance
👉 Same method name (pay)
👉 Different implementation in child classes
👉 Decided at runtime → Method Overriding
Difference Between Overloading & Overriding
| Feature | Overloading | Overriding |
|---|---|---|
| Type | Compile-Time | Runtime |
| Method Name | Same | Same |
| Parameters | Must differ | Must be same |
| Inheritance | Not required | Required |
| Polymorphism | Static | Dynamic |
Advantages of Polymorphism
✔ Removes complexity
✔ Increases code flexibility
✔ Makes code more maintainable
✔ Reduces duplication
✔ Enables dynamic behavior in programs
Conclusion :
Polymorphism is a powerful OOP feature in Java that allows the same method name to behave differently depending on the context.
-
Method Overloading → Compile-Time Polymorphism
-
Method Overriding → Runtime Polymorphism
Using polymorphism makes your code cleaner, flexible, reusable, and professional — widely used in real-world applications.
You have now mastered Polymorphism in Java with a real-world, scalable payment system that top companies actually use!
Keep practicing — this is how professional Java code is written.
Happy coding! ![]()
.png)