Instagram
youtube
Facebook
Twitter

Polymorphism

Polymorphism is a concept in object-oriented programming that allows objects of different classes to be treated as if they were of the same class. It is achieved through the use of inheritance, overriding, and method overloading.

In Python, polymorphism is supported natively, and it allows us to write code that can work with different types of objects in a flexible and efficient way.
Here is an example of polymorphism in Python:

class Shape:
    def calculate_area(self):
        pass

class Rectangle(Shape):
    def __init__(self, length, width):
        self.length = length
        self.width = width
    
    def calculate_area(self):
        return self.length * self.width

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius
    
    def calculate_area(self):
        import math
        return math.pi * self.radius ** 2

# Function to calculate the area of any shape without knowing its specific type
def get_area(shape):
    return shape.calculate_area()

# Creating instances of the classes
rectangle = Rectangle(5, 10)
circle = Circle(7)

# Using the get_area function to calculate the area of different shapes
print(f"Area of rectangle: {get_area(rectangle)}")
print(f"Area of circle: {get_area(circle)}")

In this example, the Animal class is an abstract class that defines the speak() method as an abstract method using the NotImplementedError exception. This means that any subclass of Animal must implement the speak() method.

The Dog and Cat classes are subclasses of Animal that override the speak() method to return the sound of a dog and a cat, respectively.

The animal_speak() function takes an Animal object as a parameter and calls its speak() method. Because both Dog and Cat classes inherit from the Animal class and override its speak() method, they can be treated as if they were of the same class. This is an example of polymorphism in Python.

In addition to method overriding, Python also supports method overloading through the use of default arguments and variable-length arguments. This allows methods with the same name to accept different numbers and types of arguments, making them more flexible and versatile.