File tree Expand file tree Collapse file tree 3 files changed +34
-23
lines changed Expand file tree Collapse file tree 3 files changed +34
-23
lines changed Original file line number Diff line number Diff line change 11class Animal :
2- def __init__ (self , living_place ):
3- self .living_place = living_place
2+ def __init__ (self , habitat ):
3+ self .habitat = habitat
4+
5+ def print_habitat (self ):
6+ print (self .habitat )
7+
8+ def sound (self ):
9+ print ("Some Animal Sound" )
410
5- def printplace (self ):
6- print (self .living_place )
711
812class Dog (Animal ):
9- def __init__ (self , living_place ):
10- super ().__init__ (living_place )
13+ def __init__ (self ):
14+ super ().__init__ ("Kennel" )
15+
16+ def sound (self ):
17+ print ("Woof woof!" )
1118
12- x = Dog ("zoo" )
13- x .printplace ()
1419
20+ x = Dog ()
21+ x .print_habitat ()
22+ x .sound ()
Original file line number Diff line number Diff line change 11## Exercise: Inheritance
22
3- 1 . create inheritance using animal dog relation.
3+ 1 . create inheritance using animal Dog relation.
44
55
66```
77for example,
8- animal and dog both has same living_place so create a method for living_place
8+ Animal and Dog both has same habitat so create a method for habitat
99```
1010
11112 . use super() constructor for calling parent constructor.
1212
1313```
14- class animal :
14+ class Animal :
1515 #code
1616
17- class dog(animal ):
18- super()-it refers animal class,now you can call animal 's methods.
17+ class Dog(Animal ):
18+ super()-it refers Animal class,now you can call Animal 's methods.
1919```
2020
2121[ Solution] ( https://github.com/codebasics/py/blob/master/Basics/python_basics/17_inheritance/17_inheritance.py )
Original file line number Diff line number Diff line change 1- class Teacher ():
2- def teachers_action (self ):
3- print (' I can teach' )
1+ class Teacher :
2+ def teachers_action (self ):
3+ print (" I can teach" )
44
55
6- class Engineer ():
6+ class Engineer :
77 def Engineers_action (self ):
8- print (' I can code' )
8+ print (" I can code" )
99
10- class Youtuber ():
10+
11+ class Youtuber :
1112 def youtubers_action (self ):
12- print ('I can code and teach' )
13+ print ("I can code and teach" )
14+
15+
16+ class Person (Teacher , Engineer , Youtuber ):
17+ pass
1318
1419
15- class Person (Teacher , Engineer , Youtuber ):
16- pass
17- coder = Person ()
20+ coder = Person ()
1821coder .teachers_action ()
1922coder .Engineers_action ()
2023coder .youtubers_action ()
You can’t perform that action at this time.
0 commit comments