Polymorphism is one of the core concepts of Object-Oriented Programming (OOP) in Java.
It
allows objects of different classes to be treated as objects of a common superclass. There are
two types of polymorphism in Java:
1. Compile-time Polymorphism (Method Overloading)
2. Runtime Polymorphism (Method Overriding)
1. Compile-time Polymorphism (Method Overloading)
Method overloading occurs when multiple methods in the same class share the same name but
have different parameter lists.
Example of Method Overloading
class MathOperations {
// Overloaded method with two integers
int add(int a, int b) {
return a + b;
}
// Overloaded method with three integers
int add(int a, int b, int c) {
return a + b + c;
}
// Overloaded method with double values
double add(double a, double b) {
return a + b;
}
}
public class OverloadingExample {
public static void main(String[] args) {
MathOperations math = new MathOperations();
System.out.println("Sum of 2 integers: " + math.add(5, 10));
System.out.println("Sum of 3 integers: " + math.add(5, 10,
15));
System.out.println("Sum of 2 doubles: " + math.add(5.5,
10.5));
}
}
Output:
Sum of 2 integers: 15
Sum of 3 integers: 30
Sum of 2 doubles: 16.0
2. Runtime Polymorphism (Method Overriding)
Method overriding allows a subclass to provide a specific implementation of a method already
defined in its superclass.
Example of Method Overriding
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
@Override
void makeSound() {
System.out.println("Cat meows");
}
}
public class OverridingExample {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myDog = new Dog(); // Upcasting
Animal myCat = new Cat(); // Upcasting
myAnimal.makeSound();
myDog.makeSound();
myCat.makeSound();
}
}
Output:
Animal makes a sound
Dog barks
Cat meows
3. Real-World Example: Payment Processing System
Here’s a real-world example of polymorphism used in a payment processing system:
abstract class Payment {
abstract void makePayment(double amount);
}
class CreditCardPayment extends Payment {
@Override
void makePayment(double amount) {
System.out.println("Payment of $" + amount + " made using
Credit Card.");
}
}
class PayPalPayment extends Payment {
@Override
void makePayment(double amount) {
System.out.println("Payment of $" + amount + " made using
PayPal.");
}
}
public class PaymentSystem {
public static void main(String[] args) {
Payment payment1 = new CreditCardPayment();
Payment payment2 = new PayPalPayment();
payment1.makePayment(100.50);
payment2.makePayment(200.75);
}
}
Output:
Payment of $100.5 made using Credit Card.
Payment of $200.75 made using PayPal.
Key Takeaways
✔ Method Overloading allows multiple methods in the same class with the same name but
different parameters (Compile-time).
✔ Method Overriding allows a subclass to provide a different implementation of a method
already defined in its superclass (Runtime).
✔ Polymorphism increases code reusability and flexibility, making the system scalable
and maintainable.
OOP Exam (Polymorphism): Payment
Processing System with Discounts
🔹 Objective:
Implement an advanced payment processing system using:
✔ Abstract Classes & Interfaces
✔ Method Overriding & Polymorphism
✔ Exception Handling
✔ Mathematical Formula (Discount Calculation)
Features in This Code:
✔ Abstract Classes & Interfaces (Encapsulation & Polymorphism)
✔ Method Overriding (Dynamic Behavior)
✔ Exception Handling (Validation for Payment Amounts)
✔ Mathematical Formula (Discount Calculation Based on Payment Amount)
✔ Loop for Multiple Transactions (Users can continue making payments until they exit)
📌 Instructions
Part 1: Design the System
1. Create an interface named PaymentGateway with a method boolean
validatePayment(double amount).
2. Create an abstract class named Payment that:
○ Implements PaymentGateway
○ Defines an abstract method processPayment(double amount).
○ Includes a method double calculateDiscount(double amount) that
applies a discount based on:
■ 5% discount if amount > $500
■ 10% discount if amount > $1000
■ No discount otherwise.
3. Create three payment method classes (CreditCardPayment, PayPalPayment,
BankTransferPayment) that:
○ Extend Payment and override processPayment(double amount).
○ Implement validatePayment(double amount) to ensure the amount is
greater than zero.
4. Implement exception handling for invalid payment amounts.
5. Display the discounted amount before processing the payment.
📌 Example Interaction (User Input & Output)
Select a payment method:
1 - Credit Card
2 - PayPal
3 - Bank Transfer
Enter your choice: 2
Enter the amount to pay: 1200.00
Original Amount: $1200.0
Discount Applied: 10% ($120.0)
Final Amount to Pay: $1080.0
Processing payment using PayPal. Amount: $1080.0
Advanced Challenge (Bonus Points)
✔ Modify the program to allow multiple transactions in a loop until the user chooses to exit.
✔ Add an additional payment method like CryptocurrencyPayment that offers a 15%
discount for amounts over $2000.
📌 Expected Output (With Discount)
Test Case 1: Credit Card Payment (No Discount)
yaml
CopyEdit
Select a payment method:
1 - Credit Card
2 - PayPal
3 - Bank Transfer
Enter your choice: 1
Enter the amount to pay: 300.00
Original Amount: $300.0
Discount Applied: $0.0
Final Amount to Pay: $300.0
Processing payment using Credit Card. Amount: $300.0
Payment successful!
Test Case 2: PayPal Payment (5% Discount)
yaml
CopyEdit
Select a payment method:
1 - Credit Card
2 - PayPal
3 - Bank Transfer
Enter your choice: 2
Enter the amount to pay: 800.00
Original Amount: $800.0
Discount Applied: $40.0
Final Amount to Pay: $760.0
Processing payment using PayPal. Amount: $760.0
Payment successful!
Test Case 3: Bank Transfer (10% Discount)
yaml
CopyEdit
Select a payment method:
1 - Credit Card
2 - PayPal
3 - Bank Transfer
Enter your choice: 3
Enter the amount to pay: 1500.00
Original Amount: $1500.0
Discount Applied: $150.0
Final Amount to Pay: $1350.0
Processing payment using Bank Transfer. Amount: $1350.0
Payment successful!
📌 Summary of Key Concepts Covered
✔ Interfaces (Enforcing validatePayment(double amount))
✔ Abstract Classes & Method Overriding (processPayment(double amount))
✔ Polymorphism (Using a Payment reference for different subclasses)
✔ Exception Handling (Invalid amount detection)
✔ Mathematical Formula (Applying dynamic discount rates)