Let's talk about another feature of Object-Oriented Programming that's really, really handy.
And it's something that we call Class Inheritance.
How does this work?
Well, let's say that you code up a Robot Chef and you give it a bunch of functionality like you tell
it how to bake, how to stir, how to measure,
and then at some point, you decide actually, you know, in my restaurant, I also need a Pastry Chef.
Now, this pastry chef will need to know some of the things that a chef knows how to do, but it also
needs something extra.
So you don't want to create this pastry chef entirely from scratch.
Instead, what you want to do is you want to be able to take the methods you already defined for the
Chef class and then just add to it.
Add a few other relevant methods like how to knead dough, or how to whisk some eggs.
So this process of inheriting behavior and appearance from an existing class is known as Class Inheritance.
When we're talking about class inheritance, you can inherit both appearance,
so attributes, like for example, if you inherited the same eyes as your mother, or if you inherited
the same nose as your grandfather, but you can also inherit behavior.
So maybe you chop tomatoes in the same way that your dad chops tomatoes.
And if you believe my mother, I'm lazy in the same way that my dad is lazy.
So not only can win inherit appearance, but we can also inherit behavior.
Now, how does inheritance actually work in terms of the code?
Well, we can define a class, let's call it Fish,
and it has an initializer.
Now, in order to get this Fish class to inherit from another class, all we have to do is to add a
set of parentheses after the name of the class, and then provide the class that we want to inherit
from.
So in this case, our Fish is inheriting from the Animal class.
And then in order to get hold of everything that an animal has and is so its attributes and methods,
all we have to do is inside the __init__(),
add this super().__init__(), and this super refers to the superclass.
So basically initialize everything that the superclass can do in our Fish class.
Let's take a look at this in terms of real code.
First I'm going to create an Animal class,
and this Animal class is going to have an initializer where we're able to define some attributes.
Right?
So let's say that all animals have two eyes,
so num_eyes = 2.
Now at the same time, I'm also going to define a method associated with this Animal class.
Let's say that all animals know how to breathe.
So inside this breathe() method, I'm going to keep it quite simple,
I'm just going to say, ("Inhale, exhale").
So that's breathing done.
Now later on I decide to define a Fish class,
and this Fish class probably also knows how to do certain things.
So for example maybe it knows how to swim, right?
Right
now if I create an object from this Fish class, let's say I create an object called nemo that's created
from the Fish class,
then I can say nemo.swim().
And you can see when I run this code it says that nemo is moving in water.
Now what if I wanted this Fish class to inherit everything that the Animal class can do, including
the attribute, num_eyes, and also the method breathe()?
All I have to do is to add this Animal class after the name of the Fish class inside parentheses, and
then I have to add the initializer and inside the initializer, I have to trigger a call to the superclass(),
which is in this case the Animal class,
and then I'm going to call the initializer like this.
So now what these two parts of the code does is it allows anything that's created from my Fish class
to inherit all of the attributes and methods from the superclass, which is the Animal class.
Now I can get my nemo to start breathing and you can see if I hit Run, then it will also Inhale and
exhale.
And in addition, if I wanted to print, nemo.num_eyes then you can see this is also going to print
too.
So now my object that's created from the Fish class now has access to all the attributes and methods
from the superclass that inherited from the Animal class.
Now what if I wanted to inherit a method so get all the things that it does, for example in the breathe()
method,
but what if I wanted to modify it a little bit?
So for example, a Fish does in fact breathe, but it kind of breathes underwater.
So let's say I wanted to define the breathe() function,
and I want it to have the same functionality as the superclass which I'm inheriting from.
So I want to print, inhale, exhale,
but I also want to do something extra.
Well in this case we would get hold of the superclass and then call breathe() on it.
This means we're going to do everything that the breathe() method from the superclass does.
So all of this.
But afterwards we're going to do something a little bit more special.
So we're going to say, "We're doing this underwater."
Now when this line nemo. breathe() gets run, you can see what happens is it will print out Inhale,
exhale,
and this comes from the superclass's breathe() method here.
And then afterwards it will print its own unique twist on breathing, which is, "doing this underwater."
By learning about inheritance,
what it allows us to do is to take an existing class that we've created, or somebody else has created,
and then build on top of it without having to reinvent the wheel and redefine everything that's in that
class.
So in the next lesson, I'm going to show you how we can inherit from the turtle class, to upgrade
the turtle, to be able to do extra things, such as creating a piece of food, or creating a scoreboard
For all of that and more,
I'll see you on the next lesson.