-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path6th.py
52 lines (41 loc) · 1.22 KB
/
6th.py
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
44
45
46
47
48
49
50
51
52
# Parent class
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print(self.name + " makes a sound.")
# Child class (inheritance type: single inheritance)
class Dog(Animal):
def __init__(self, name):
super().__init__(name)
def speak(self):
print(self.name + " barks.")
# Child class (inheritance type: multi-level inheritance)
class GoldenRetriever(Dog):
def __init__(self, name):
super().__init__(name)
def speak(self):
print(self.name + " barks and wags its tail.")
# Child class (inheritance type: multiple inheritance)
class Bird:
def __init__(self, name):
self.name = name
def speak(self):
print(self.name + " chirps.")
class Parrot(Bird, Animal):
def __init__(self, name):
super().__init__(name)
def speak(self):
print(self.name + " repeats words.")
# create objects of each class
animal = Animal("Animal")
dog = Dog("Dog")
golden = GoldenRetriever("Golden Retriever")
bird = Bird("Bird")
parrot = Parrot("Parrot")
# call the speak method of each object
animal.speak()
dog.speak()
golden.speak()
bird.speak()
parrot.speak()