Polymorphism-OOP
Polymorphism-OOP
It
allows objects of different classes to be treated as objects of a common superclass. There are
two types of polymorphism in Java:
class MathOperations {
// Overloaded method with two integers
int add(int a, int b) {
return a + b;
}
Output:
Sum of 2 integers: 15
Sum of 3 integers: 30
Sum of 2 doubles: 16.0
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
myAnimal.makeSound();
myDog.makeSound();
myCat.makeSound();
}
}
Output:
payment1.makePayment(100.50);
payment2.makePayment(200.75);
}
}
Output:
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)
📌 Instructions
Part 1: Design the System
1 - Credit Card
2 - PayPal
3 - Bank Transfer