-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathinheritance_polymorphism.py
119 lines (81 loc) · 3.16 KB
/
inheritance_polymorphism.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
Inheritance:
------------
All species inherit a lot from their parents, may be they have same eyes as
their mother but different voice all together.
Python classes are no different, we can inherit from classes and make them share
common functionality. At the same time we can dynamically add other features and
functionality as well.
Polymorphism:
-------------
Suppose there are two children, one want's to speak in Marathi, other want's to
speak in Sanskrit. This is possible using polymorphism! It's just creating the same
methods but with different behavior.
"""
# -------------------------------------------------------------------------
class Animal:
def __init__(self, name: str, age: int, num_legs: int) -> None:
# Create & initialize instance variables
self.name = name
self.age = age
self.num_legs = num_legs
def __str__(self) -> str:
return f"Name: {self.name}"
def talk(self) -> None:
"""Makes the animal talk"""
print(f"{self.name} can't talk yet!")
a1 = Animal("Robbin", 10, 4)
print(a1)
a1.talk()
# -------------------------------------------------------------------------
class Dog(Animal):
def __init__(self, name: str, age: int, num_legs: int, breed: str) -> None:
# Take the common features and pass to the parent(super) class
super().__init__(name, age, num_legs)
# Define custom instance variables
self.breed = breed
self.type = "Dog"
def __str__(self) -> str:
return f"{self.type}: {self.name}, Breed: {self.breed}"
# We alter the talk method and make it say woff adding polymorphic behavior
def talk(self) -> None:
print(f"{self.name} says wofff...")
def sniff(self, item: str) -> None:
print(f"{self.name} is sniffing out {item}")
d1 = Dog("Whisky", age=5, num_legs=4, breed="Doberman")
print(d1)
d1.talk()
d1.sniff("ball")
# -------------------------------------------------------------------------
class Cat(Animal):
def __init__(self, name: str, age: int, num_legs: int, breed: str) -> None:
super().__init__(name, age, num_legs)
self.breed = breed
self.type = "Cat"
def __str__(self) -> str:
return f"{self.type}: {self.name}, Breed: {self.breed}"
def talk(self) -> None:
print(f"{self.name} says meow...")
c1 = Cat("Jess", age=2, num_legs=4, breed="Persian Cat")
print(c1)
c1.talk()
# -------------------------------------------------------------------------
class Dino(Animal):
def __init__(self, name: str, age: int, num_legs: int, breed: str) -> None:
super().__init__(name, age, num_legs)
self.breed = breed
self.type = "Dino"
def __str__(self) -> str:
return f"{self.type}: {self.name}, Breed: {self.breed}"
def talk(self) -> None:
print(f"{self.name} says urrghhhh...")
def hunt(self) -> None:
print(f"{self.name} is out for hunting...")
dino1 = Dino("Adam", age=8, num_legs=2, breed="T-Rex")
print(dino1)
dino1.talk()
dino1.hunt()
# -------------------------------------------------------------------------
print(isinstance(d1, Animal))
print(isinstance(d1, Dog))
print(isinstance(d1, Cat))