Introduction
Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. It enables the same method to be used on different objects, making the code more flexible and reusable.
Key Concepts
Method Overriding
Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. The method in the subclass overrides the method in the superclass.
Polymorphic Functions
Polymorphic functions are functions that can operate on objects of different classes as long as they share a common interface.
Example: Method Overriding
Parent Class
class Animal:
def make_sound(self):
return "Some generic sound"
Child Classes
class Dog(Animal):
def make_sound(self):
return "Bark"
class Cat(Animal):
def make_sound(self):
return "Meow"
Using Method Overriding
def animal_sound(animal):
print(animal.make_sound())
# Creating objects of Dog and Cat classes
dog = Dog()
cat = Cat()
# Using the polymorphic function
animal_sound(dog) # Output: Bark
animal_sound(cat) # Output: Meow
Explanation
- Parent Class (
Animal
): Defines a methodmake_sound
that returns a generic sound. - Child Classes (
Dog
andCat
): Override themake_sound
method to return specific sounds. - Polymorphic Function (
animal_sound
): Takes ananimal
object and calls itsmake_sound
method. The actual method called depends on the object’s class.
Example: Polymorphic Functions
Let’s consider a real-world example of a payment system where different payment methods can be used.
Parent Class: Payment
class Payment:
def pay(self, amount):
pass
Child Classes: CreditCardPayment
and DebitCardPayment
class CreditCardPayment(Payment):
def pay(self, amount):
return f"Paid {amount} using Credit Card"
class DebitCardPayment(Payment):
def pay(self, amount):
return f"Paid {amount} using Debit Card"
Using Polymorphic Functions
def process_payment(payment_method, amount):
print(payment_method.pay(amount))
# Creating objects of CreditCardPayment and DebitCardPayment classes
credit_card_payment = CreditCardPayment()
debit_card_payment = DebitCardPayment()
# Using the polymorphic function
process_payment(credit_card_payment, 1000) # Output: Paid 1000 using Credit Card
process_payment(debit_card_payment, 2000) # Output: Paid 2000 using Debit Card
Explanation
- Parent Class (
Payment
): Defines a methodpay
as an interface. - Child Classes (
CreditCardPayment
andDebitCardPayment
): Implement thepay
method to perform specific payment actions. - Polymorphic Function (
process_payment
): Takes apayment_method
object and calls itspay
method. The actual method called depends on the object’s class.
Real-World Example: Shape Drawing
Let’s consider a real-world example of drawing different shapes using polymorphism.
Parent Class: Shape
class Shape:
def draw(self):
pass
Child Classes: Circle
and Rectangle
class Circle(Shape):
def draw(self):
return "Drawing a Circle"
class Rectangle(Shape):
def draw(self):
return "Drawing a Rectangle"
Using Polymorphic Functions
def draw_shape(shape):
print(shape.draw())
# Creating objects of Circle and Rectangle classes
circle = Circle()
rectangle = Rectangle()
# Using the polymorphic function
draw_shape(circle) # Output: Drawing a Circle
draw_shape(rectangle) # Output: Drawing a Rectangle
Explanation
- Parent Class (
Shape
): Defines a methoddraw
as an interface. - Child Classes (
Circle
andRectangle
): Implement thedraw
method to draw specific shapes. - Polymorphic Function (
draw_shape
): Takes ashape
object and calls itsdraw
method. The actual method called depends on the object’s class.
Conclusion
Polymorphism in Python allows objects of different classes to be treated as objects of a common superclass. It enables the same method to be used on different objects, making the code more flexible and reusable. By understanding and utilizing polymorphism, you can design more modular and maintainable object-oriented systems. The provided examples of method overriding and polymorphic functions demonstrate how polymorphism can be effectively applied in real-world scenarios.