Python Polymorphism

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

  1. Parent Class (Animal): Defines a method make_sound that returns a generic sound.
  2. Child Classes (Dog and Cat): Override the make_sound method to return specific sounds.
  3. Polymorphic Function (animal_sound): Takes an animal object and calls its make_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

  1. Parent Class (Payment): Defines a method pay as an interface.
  2. Child Classes (CreditCardPayment and DebitCardPayment): Implement the pay method to perform specific payment actions.
  3. Polymorphic Function (process_payment): Takes a payment_method object and calls its pay 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

  1. Parent Class (Shape): Defines a method draw as an interface.
  2. Child Classes (Circle and Rectangle): Implement the draw method to draw specific shapes.
  3. Polymorphic Function (draw_shape): Takes a shape object and calls its draw 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top