-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.py
More file actions
43 lines (31 loc) · 878 Bytes
/
inheritance.py
File metadata and controls
43 lines (31 loc) · 878 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Animal:
def __init__(self, name="Generic Animal"):
self.name = name
def eat(self):
return f"{self.name} eats"
def sleep(self):
return f"{self.name} sleeps"
def speak(self):
return f"{self.name} makes a sound"
class Dog(Animal):
def bark(self):
return "Woof!"
class Cat(Animal):
def meow(self):
return "Meow!"
class Bird(Animal):
def chirp(self):
return "Chirp!"
# Example usage:
dog = Dog()
cat = Cat()
bird = Bird()
print(dog.speak()) # Output: Woof!
print(dog.bark()) # Output: Woof!
print(cat.speak()) # Output: Meow!
print(bird.speak()) # Output: Chirp!
print(dog.eat()) # Output: Animal eats
print(cat.sleep()) # Output: Animal sleeps
print(bird.eat()) # Output: Animal eats
print(dog.sleep()) # Output: Animal sleeps
print(cat.eat()) # Output: Animal eats