Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 7: Wizard Battle App
Lecture: Concept: Inheritance
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Inheritance is a core concept of all object oriented programming and it's exactly the tool that we need,
0:07
to help us build these special types of creatures in our game, dragons, small animals and so on.
0:14
The way it works is you declare what is called a base type, in our case this is the creature and it has all the common functionality.
0:21
But we can add specialization to it, we can add some other type that is like a creature but has additional pieces of information, or behaviors,
0:30
so here we have a dragon and the way we declare its base type, the thing that it gets its common behaviors from is we say "(" and the class name, ")".
0:40
So dragon is a creature but dragons also have special behaviors and data so this dragon can bread fire, you can see there is breed fire method,
0:49
creatures can do that, it's adding that to its specialization, and it also has a different set of data,
0:54
it has a scale thickness in addition to the name and level. Now, notice this call to super, we say super.init,
1:01
that's the initializer for the creature class, and we have to pass the name and the level onto this base class, we technically could ignore this step
1:09
and then just store the name and level as well, but you often find you would be duplicating code
1:15
and introducing various bugs by omission and so on, if you do that. Here we are actually letting the creature class
1:23
deal with storing and processing the name and level and whatever that means and however that evolves over time,
1:28
and we are only doing the extra stuff in the dragon, like storing the scale thickness and so on, so let's go back and apply this concept of inheritance
1:35
to our wizard game.