#100DaysOfWeb in Python Transcripts
Chapter: Appendix: Python language concepts
Lecture: Concept: Inheritance
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
A key design feature for working with classes and object-oriented programming is modeling and layers, going from the most general to the most specific.
0:10
So, we started with a creature class, and a creature class has a name and a level and it's just a generic creature,
0:17
it could be anything, so it could be a squirrel as we saw, it could be a dragon, it could be a toad.
0:24
Any kind of creature we can think of, we could model with the original creature class,
0:27
and that's great because it's very applicable but there are differences between a dragon and a toad, for example,
0:34
maybe the dragon breathes fire, not too many toads breed fire,
0:37
and so we can use inheritance to add additional specializations to our more specific types,
0:44
so we can have a specific dragon class, which can stand in for a creature, it is a creature but it also has more behaviors and more variables.
0:52
Here we have our initializer, the __init__ and you see we take the required parameters and data to pass along to the creature class,
1:01
in order to create a creature, in order for the dragon to be a creature, it has to supply a name and a level,
1:06
so we can get to the creature's initializer saying super().__init__ and pass name and level and that allows the creature to do
1:13
whatever sort of setup it does when it gets created, but we also want to have a scale thickness for our dragon,
1:18
so we create another field specific only to dragons, and we say self.scale_thickness = whatever they passed in.
1:24
So in addition to having name and level we get from Creature, we also have a scale thickness,
1:29
so that adds more data we can also add additional behaviors, here we have added a breed_fire method. So the way we create a derived type in Python,
1:37
is we just say class, because it is a class, the name of the class, Dragon, and in parenthesis the name of the base type.
1:45
And then, other than that, and using "super", this is basically the same as creating any other class.