-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path7th.py
More file actions
33 lines (26 loc) · 675 Bytes
/
7th.py
File metadata and controls
33 lines (26 loc) · 675 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
# Parent class
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print(self.name + " makes a sound.")
# Child class
class Dog(Animal):
def __init__(self, name):
super().__init__(name)
def speak(self):
print(self.name + " barks.")
# Child class
class Cat(Animal):
def __init__(self, name):
super().__init__(name)
def speak(self):
print(self.name + " meows.")
# create objects of each class
animal = Animal("Animal")
dog = Dog("Dog")
cat = Cat("Cat")
# call the speak method of each object
animal.speak()
dog.speak()
cat.speak()