169. Polymorphism in Python
class Animal:
def sound(self):
return "Some sound"
class Dog(Animal):
def sound(self):
return "Bark"
class Cat(Animal):
def sound(self):
return "Meow"
animals = [Dog(), Cat()]
for animal in animals:
print(animal.sound())class Rectangle:
def area(self, length, width):
return length * width
class Circle:
def area(self, radius, _=None):
return 3.14 * radius * radius
def print_area(shape, *args):
print("Area:", shape.area(*args))
print_area(Rectangle(), 5, 10)
print_area(Circle(), 7)Last updated