-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritance and polymorphism OOP.py
More file actions
207 lines (96 loc) · 2.49 KB
/
Inheritance and polymorphism OOP.py
File metadata and controls
207 lines (96 loc) · 2.49 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python
# coding: utf-8
# In[1]:
class Animal():
def __init__(self):
print("Animal Created")
def who_am_i(self):
print("I am an animal")
def eat(self):
print("I am eating")
# # Inheritence
# In[6]:
# Dog is inhereting the Animal class and Dog class than can call the functions and attribute except private
class Dog(Animal):
def __init__(self):
print("Dog created")
Animal.__init__(self)
def bark(self):
print("wofff !")
# ovverriting the method
def eat(self):
print("I am a dog and eating")
# In[7]:
myDog = Dog()
# In[8]:
# you can see that Dog can call the animal functions because of inheritence
myDog.eat()
# In[9]:
myDog.bark()
# # Polmorphism
# In[17]:
# Difference object classes can share the same method name
# those method can be called form same place even though a variety of different object
# example
class Cat():
def __init__(self,name):
print("Cat created")
self.name = name
def speak(self):
return self.name + " Says meoww !"
# In[18]:
class Dog():
def __init__(self,name):
print("Dog created")
self.name = name
def speak(self):
return self.name + " Says Woff !"
# In[19]:
dog = Dog("Tommy")
# In[20]:
cat = Cat("catty")
# In[21]:
dog.speak()
# In[22]:
cat.speak()
# In[26]:
# both animal have speak method so create base class Animal and put this method on that class and override to any instance
class Animal():
def __init__(self):
print("Animal Created")
def who_am_i(self):
print("I am an animal")
# this is an abstract method
def speak(self):
raise NotImplementedError("sub class must implment this")
# In[24]:
dog = Dog("Tommy")
# In[25]:
dog.speak()
# In[27]:
animal = Animal()
# In[29]:
# it will return error
animal.speak()
# In[31]:
# than inherit the Animal class
class Cat(Animal):
def __init__(self,name):
print("Cat created")
self.name = name
def speak(self):
return self.name + " Says meoww !"
# In[32]:
class Dog(Animal):
def __init__(self,name):
print("Dog created")
self.name = name
def speak(self):
return self.name + " Says Woff !"
# In[34]:
dog = Dog("Tommy")
dog.speak()
# In[35]:
cat = Cat("catty")
cat.speak()
# In[ ]: