top of page

Mastering Polymorphism in Java: Real-World Payment System Example with Deep Dive

Updated: 6 days ago

1. What Is Polymorphism?

Polymorphism means "many forms" — the ability of an object to take on multiple behaviors through a common interface or superclass.

💡 Analogy:

You have a remote control (interface reference), but the actual device it controls (subclass object) could be a TV, an AC, or a sound system. The remote is the same, but the behavior changes depending on the actual device (subclass).


✅ In Java:

You can use one reference type (like a base class) to point to objects of different subtypes — and invoke different behaviors based on the actual object type.



Real-World Scenario: Payment System

💡 Scenario:

A large e-commerce app accepts multiple payment methods:

  • Credit Card

  • PayPal

  • UPI

Even though each payment type has different processing logic, we want to treat them the same way in code — this is polymorphism in action!



Advantages of Polymorphism

Advantage

Description

Code Reusability

You can reuse code that works with a superclass or interface, and it will work with all subclasses.

Extensibility

You can add new subclasses without modifying existing code — Open/Closed Principle.

Loose Coupling

Promotes programming to interfaces, making code more flexible and testable.

Simplifies Code

Enables writing simpler, cleaner code that works on abstract types instead of concrete implementations.

Improves Maintainability

Changes to a subclass do not affect the superclass or unrelated subclasses.



🔨 3. Code Example (Java 21 Compatible)

🧱 Step 1: Base Class / Interface public interface PaymentMethod {

void pay(double amount);

}


🏦 Step 2: Subclasses with Different Implementations public class CreditCardPayment implements PaymentMethod {

public void pay(double amount) {

System.out.println("Paid ₹" + amount + " using Credit Card.");

}

}


public class PayPalPayment implements PaymentMethod {

public void pay(double amount) {

System.out.println("Paid ₹" + amount + " using PayPal.");

}

}


public class UpiPayment implements PaymentMethod {

public void pay(double amount) {

System.out.println("Paid ₹" + amount + " using UPI.");

}

}


🧑‍💻 Step 3: Use Polymorphism in Business Logic public class PaymentProcessor {


public void processPayment(PaymentMethod method, double amount) {

method.pay(amount); // Polymorphic call

}


public static void main(String[] args) {

PaymentProcessor processor = new PaymentProcessor();


processor.processPayment(new CreditCardPayment(), 1500.0);

processor.processPayment(new PayPalPayment(), 900.0);

processor.processPayment(new UpiPayment(), 400.0);

}

}


📘 4. What's Happening Behind the Scenes? PaymentMethod method = new PayPalPayment(); // Base type reference

method.pay(500); // Calls PayPalPayment's pay() method The JVM uses dynamic dispatch to call the correct pay() method based on the actual object, not the reference type.

This is runtime polymorphism. 📦 5. Types of Polymorphism

Type

Description

Example

Compile-time (static)

Method overloading

print(int) vs print(String)

Runtime (dynamic)

Method overriding

Subclass overrides parent method — like pay() above

✅ 6. Why Is This Powerful?

  • Code flexibility: You can add new payment types (like Crypto) without changing existing logic.

  • Open-Closed Principle: Add new behavior by extending, not modifying.

  • Reusable design: Can handle many subtypes via one interface.

  • In interviews: Common in design questions (e.g., strategy pattern, microservice integration). 🧠 Real-World Examples of Polymorphism

Scenario

Description

Shape Drawing App

Shape interface with draw() — implemented by Circle, Rectangle, etc.

Vehicle System

Vehicle → Car, Truck, Bike with overridden startEngine()

Notification System

Notifier → EmailNotifier, SMSNotifier, PushNotifier

Banking App

Account → SavingsAccount, CurrentAccount, LoanAccount with different calculateInterest() logic

✅ Summary

Concept

Key Point

Polymorphism

One interface, many implementations

Java Support

Method overriding via interface or inheritance

Real-World Fit

Flexible, extensible systems (payment, drawing, notification)

Interview Use

Strategy pattern, dynamic behavior


Recent Posts

See All
Inheritance in Java

🎯 Goal of Inheritance Inheritance enables code reuse  and creates a hierarchical relationship  between classes. It allows a subclass to...

 
 
 
Encapsulation with Getters/Setters

🎯 Goal of Encapsulation Encapsulation is the practice of hiding internal object state  and exposing access via controlled methods...

 
 
 

Comments


bottom of page