How to implement polymorphism in Python?

85 views

How to implement polymorphism in Python?

How to implement polymorphism in Python?

solveurit24@gmail.com Changed status to publish February 13, 2025
0

Polymorphism occurs when different classes have the same method names and overriding parent class methods.

class Shape:
    def area(self):
        pass

class Circle(Shape):
    def area(self, radius):
        return 3.14 * radius * radius

class Square(Shape):
    def area(self, length):
        return length * length

circle = Circle()
square = Square()
print(circle.area(5))  # Output: 78.5
print(square.area(4))  # Output: 16

solveurit24@gmail.com Changed status to publish February 13, 2025
0
You are viewing 1 out of 1 answers, click here to view all answers.