Skip to content

Commit 87f14c1

Browse files
Merge pull request #22 from basnetsoyuj/master
Added 22-inheritance.md
2 parents 9d49ef5 + 0229a2d commit 87f14c1

File tree

1 file changed

+345
-0
lines changed

1 file changed

+345
-0
lines changed

22-inheritance.md

+345
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
# Python Inheritance
2+
3+
**Video link:** [https://youtu.be/C8qE3mKiBrQ](https://youtu.be/C8qE3mKiBrQ)
4+
5+
In this video, you learned about inheritance in Python with the help of examples. We also covered the concepts of method overriding and the `super()` function.
6+
7+
**Programs in the Video**
8+
9+
- [Inheritance in Python](#inheritance-in-python)
10+
- [Example of Inheritance](#example-of-inheritance)
11+
- [Method Overriding](#method-overriding)
12+
- [The `super()` function](#the-super-function)
13+
14+
---
15+
16+
## Inheritance in Python
17+
Let's derive a dog and cat class from an animal class and get a feel of how inheritance works.
18+
19+
Suppose we have an `Animal` class:
20+
21+
```python
22+
class Animal:
23+
def eat(self):
24+
print("I can eat")
25+
```
26+
27+
Now, let's derive a `Dog` class from `Animal`:
28+
29+
```python
30+
class Animal:
31+
def eat():
32+
print("I can eat")
33+
34+
class Dog(Animal):
35+
def bark(self):
36+
print("I can bark")
37+
```
38+
39+
Objects of `Dog` can access methods and attributes of both `Dog` and `Animal`:
40+
41+
42+
```python
43+
class Animal:
44+
def eat():
45+
print("I can eat")
46+
47+
class Dog(Animal):
48+
def bark(self):
49+
print("I can bark")
50+
51+
dog1 = Dog()
52+
53+
dog1.bark()
54+
dog1.eat()
55+
```
56+
57+
**Output**
58+
```
59+
I can bark
60+
I can eat
61+
```
62+
63+
Similarly, if we want, we can derive another `Cat` class from the `Animal` class:
64+
65+
66+
```python
67+
class Animal:
68+
def eat():
69+
print("I can eat")
70+
71+
class Dog(Animal):
72+
def bark(self):
73+
print("I can bark")
74+
75+
class Cat(Animal):
76+
def get_grumpy(self):
77+
print("I am getting grumpy.")
78+
79+
dog1 = Dog()
80+
81+
dog1.bark()
82+
dog1.eat()
83+
84+
cat1 = Cat()
85+
cat1.eat()
86+
```
87+
88+
**Output**
89+
90+
```
91+
I can bark
92+
I can eat
93+
I can eat
94+
```
95+
96+
---
97+
98+
## Example of Inheritance
99+
100+
Let's implement a program to calculate the perimeter of different polygons like triangles and quadrilaterals using inheritance.
101+
102+
Let's first create a base class called `Polygon`:
103+
104+
```python
105+
class Polygon:
106+
def __init__(self, sides):
107+
self.sides = sides
108+
109+
def display_info(self):
110+
print("A polygon is a two dimensional shape with straight lines")
111+
112+
def get_perimeter(self):
113+
perimeter = sum(self.sides)
114+
return perimeter
115+
```
116+
117+
All polygons like triangles and quadrilaterals will derive these features.
118+
119+
Let's create a `Triangle` class that will inherit from the `Polygon` class:
120+
121+
122+
```python
123+
class Polygon:
124+
def __init__(self, sides):
125+
self.sides = sides
126+
127+
def display_info(self):
128+
print("A polygon is a two dimensional shape with straight lines")
129+
130+
def get_perimeter(self):
131+
perimeter = sum(self.sides)
132+
return perimeter
133+
134+
135+
class Triangle(Polygon):
136+
def display_info(self):
137+
print("A triangle is a polygon with 3 edges")
138+
```
139+
140+
I have redefined the `display_info()` method to display information specific to triangles.
141+
142+
Similarly, I will also define a new `Quadrilateral` class.
143+
144+
```python
145+
class Polygon:
146+
def __init__(self, sides):
147+
self.sides = sides
148+
149+
def display_info(self):
150+
print("A polygon is a two dimensional shape with straight lines")
151+
152+
def get_perimeter(self):
153+
perimeter = sum(self.sides)
154+
return perimeter
155+
156+
157+
class Triangle(Polygon):
158+
def display_info(self):
159+
print("A triangle is a polygon with 3 edges")
160+
161+
162+
class Quadrilateral(Polygon):
163+
def display_info(self):
164+
print("A quadrilateral is a polygon with 4 edges")
165+
```
166+
167+
Now, let's find the perimeter of a triangle. I will create an object from `Triangle` and use its `get_perimeter()` method.
168+
169+
170+
```python
171+
class Polygon:
172+
def __init__(self, sides):
173+
self.sides = sides
174+
175+
def display_info(self):
176+
print("A polygon is a two dimensional shape with straight lines")
177+
178+
def get_perimeter(self):
179+
value = 0
180+
for side in self.sides:
181+
value += side
182+
return value
183+
184+
185+
class Triangle(Polygon):
186+
def display_info(self):
187+
print("A triangle is a polygon with 3 edges")
188+
189+
190+
class Quadrilateral(Polygon):
191+
def display_info(self):
192+
print("A quadrilateral is a polygon with 4 edges")
193+
194+
t1 = Triangle([5, 6, 7])
195+
perimeter = t1.get_perimeter()
196+
print("Perimeter:", perimeter)
197+
```
198+
199+
**Output**
200+
201+
```
202+
Perimeter: 18
203+
```
204+
205+
---
206+
207+
## Method Overriding
208+
209+
If you have noticed, in the above example, we have the `display_info()` method in both our base class and the derived classes.
210+
211+
Let's see what will happen if we call the `display_info()` method for the `t1` triangle.
212+
213+
```python
214+
class Polygon:
215+
def __init__(self, sides):
216+
self.sides = sides
217+
218+
def display_info(self):
219+
print("A polygon is a two dimensional shape with straight lines")
220+
221+
def get_perimeter(self):
222+
perimeter = sum(self.sides)
223+
return perimeter
224+
225+
226+
class Triangle(Polygon):
227+
def display_info(self):
228+
print("A triangle is a polygon with 3 edges")
229+
230+
231+
class Quadrilateral(Polygon):
232+
def display_info(self):
233+
print("A quadrilateral is a polygon with 4 edges")
234+
235+
t1 = Triangle([5, 6, 7])
236+
perimeter = t1.get_perimeter()
237+
print("Perimeter:", perimeter)
238+
239+
t1.display_info()
240+
```
241+
242+
**Output**
243+
244+
```
245+
Perimeter: 18
246+
A triangle is a polygon with 3 edges
247+
```
248+
249+
We can see that the `display_info()` method of the `Triangle` class is called and `display_info()` of its parent class is not executed.
250+
251+
This is called method overriding.
252+
253+
**If the same method is defined in both the base and the derived class, then the method of the derived class overrides the method of the base class.**
254+
255+
256+
If we need, we can call the `display_info()` method of our parent `Polygon` class from inside its child classes like this:
257+
258+
```python
259+
class Polygon:
260+
def __init__(self, sides):
261+
self.sides = sides
262+
263+
def display_info(self):
264+
print("A polygon is a two dimensional shape with straight lines")
265+
266+
def get_perimeter(self):
267+
perimeter = sum(self.sides)
268+
return perimeter
269+
270+
271+
class Triangle(Polygon):
272+
def display_info(self):
273+
print("A triangle is a polygon with 3 edges")
274+
Polygon.display_info(self)
275+
276+
277+
class Quadrilateral(Polygon):
278+
def display_info(self):
279+
print("A quadrilateral is a polygon with 4 edges")
280+
281+
t1 = Triangle([5, 6, 7])
282+
perimeter = t1.get_perimeter()
283+
print("Perimeter:", perimeter)
284+
285+
t1.display_info()
286+
```
287+
288+
**Output**
289+
```
290+
Parameter: 18
291+
A triangle is a polygon with 3 edges
292+
A polygon is a two dimensional shape with straight lines
293+
```
294+
295+
>**Note:** `Polygon` is the name of the parent class. Since we are calling the method using a class rather than an object, we also need to pass the `self` object manually.
296+
297+
This code is a bit more unorthodox than what we have been using. There is a more elegant way to achieve the same task by using the `super()` function.
298+
299+
---
300+
301+
## The `super()` function
302+
303+
The `super()` function returns a temporary object of the superclass for a subclass.
304+
305+
```python
306+
class Polygon:
307+
def __init__(self, sides):
308+
self.sides = sides
309+
310+
def display_info(self):
311+
print("A polygon is a two dimensional shape with straight lines")
312+
313+
def get_perimeter(self):
314+
value = 0
315+
for side in self.sides:
316+
value += side
317+
return value
318+
319+
320+
class Triangle(Polygon):
321+
def display_info(self):
322+
print("A triangle is a polygon with 3 edges")
323+
# Polygon.display_info(self)
324+
super().display_info()
325+
326+
class Quadrilateral(Polygon):
327+
def display_info(self):
328+
print("A quadrilateral is a polygon with 4 edges")
329+
330+
t1 = Triangle([5, 6, 7])
331+
perimeter = t1.get_perimeter()
332+
print("Perimeter:", perimeter)
333+
334+
t1.display_info()
335+
```
336+
337+
**Output**
338+
339+
```
340+
Perimeter: 18
341+
A triangle is a polygon with 3 edges
342+
A polygon is a two dimensional shape with straight lines
343+
```
344+
345+
In this case, `super()` is an object of `Polygon`. We are using it to call `display_info()` of the `Polygon` class.

0 commit comments

Comments
 (0)