-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoops.py
More file actions
29 lines (22 loc) · 664 Bytes
/
oops.py
File metadata and controls
29 lines (22 loc) · 664 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
raise NotImplementedError("Subclass must implement abstract method")
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"
class Bird(Animal):
def speak(self):
return f"{self.name} says Tweet!"
# Creating objects of different classes
dog = Dog("Buddy")
cat = Cat("Whiskers")
bird = Bird("Tweety")
# Using polymorphism to call the same method on different objects
animals = [dog, cat, bird]
for animal in animals:
print(animal.speak())