×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

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 runtimeMethod 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!  yes


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo