Home » Posts tagged 'Python polymorphism and method overriding'

Tag Archives: Python polymorphism and method overriding

Newly Updated Posts

Python Polymorphism

Polymorphism is one of the fundamental principles of object-oriented programming (OOP) and is supported by the Python programming language. It allows objects of different classes to be treated as objects of a common superclass. Polymorphism enables you to write code that can work with objects of multiple types, providing flexibility and code reusability.

In Python, polymorphism is typically achieved through method overriding and method overloading.

  1. Method Overriding: Method overriding occurs when a subclass defines a method that is already present in its superclass with the same name and signature. The overridden method in the subclass replaces the implementation of the method in the superclass, allowing the subclass to provide its own implementation.

Here’s an example:

class Animal:
    def sound(self):
        print("Animal makes a sound")


class Cat(Animal):
    def sound(self):
        print("Cat meows")


class Dog(Animal):
    def sound(self):
        print("Dog barks")


animals = [Cat(), Dog()]

for animal in animals:
    animal.sound()

Output:
Cat meows
Dog barks

In the example above, the Animal class has a method called sound(). The Cat and Dog classes inherit from the Animal class and override the sound() method with their own implementations. When we iterate over the animals list and call the sound() method, the appropriate implementation is called based on the actual type of the object.

  1. Method Overloading (via Function Overloading): Method overloading refers to defining multiple methods with the same name but different parameters or argument types. However, Python does not support method overloading in the same way as languages like Java or C++. Instead, you can achieve a similar effect using default arguments or variable-length arguments.

Here’s an example:

class MathOperations:
    def add(self, a, b):
        return a + b

    def add(self, a, b, c):
        return a + b + c


math_ops = MathOperations()
print(math_ops.add(2, 3))       # This will raise an error

# Using variable-length arguments
class MathOperations:
    def add(self, *args):
        return sum(args)


math_ops = MathOperations()
print(math_ops.add(2, 3))       # Output: 5
print(math_ops.add(2, 3, 4))    # Output: 9

In the first example, attempting to call math_ops.add(2, 3) will raise an error because the add() method with two arguments is overwritten by the method with three arguments.

To work around this limitation, you can use variable-length arguments (*args) to define a single add() method that can accept any number of arguments. Inside the method, you can then perform the desired operation, such as summing all the arguments.

Polymorphism allows you to write code that can work with objects of different types, as long as they share a common interface or superclass. This flexibility simplifies code design, promotes code reuse, and makes it easier to extend your programs in the future.