Extending a Parent Class
00:00 So you’ve inherited a class, but what about if you want to extend the parent class, and maybe you want to override or augment its methods? Instead of just making a copy with a different name, you want to make a copy and tweak just one little aspect of it.
00:18
You can start with your parent, or base, class, Doggo
. This is unchanged from the last lesson. And then instead of creating a new class with just a pass
keyword, you can define a new method, and you’ll note that you’ve got your .speak()
method here, which takes self, sound
and returns f"{self.name} says {sound}"
.
00:47
And this new subclass overrides the .speak()
method. And it doesn’t change it much, but what it does do is gives a default argument to the sound
parameter. So now you can just say whatever the Jack Russell is—let’s say it’s miles
—and you can say .speak()
without any arguments, and the default behavior of this will be 'Miles says Arf'
, because of course Jack Russell Terriers all go a “Arf.” That said, you can still use it like the original .speak()
method, where you pass in an argument, and then Miles will say whatever you pass in.
01:29 So this is just a tiny tweak, where you’ve added a default argument instead of an argument, and you haven’t had to rewrite the whole class. You can just subclass it and override one method.
01:42
And the way you override that is just by using the same name. In the original class, it’s called the .speak()
method, and in this class, it’s called the .speak()
method. So it overrides it.
01:56
Let’s see that in action. So here you’ve got the class from the previous lesson. You’ve got your subclass. But say we want to change this slightly, and we want to change the way that .speak()
works.
02:12
What we can do is define another method here, and this will just completely override what the original method does. So let’s just say return "Hello"
for now, just to see this in action.
02:26
So Control + S, F5 to make this run Lets instantiate an object here. Let’s go with gandhi
again.
02:38
And he’s 13
. Now instead of a normal Doggo
that just speaks the sound that you pass it in, if you ask gandhi
to speak, he’ll say 'Hello'
.
02:52
But if you print gandhi
, he’ll say Gandhi is 13 years old
, like the base class. So all we’ve done here is just overridden this method.
03:04
So let’s change that a bit. sound="boof"
,
03:12 and we’ll take this and just copy it. All we’re doing is adding a default argument, and actually, let’s make this a bit different, and let’s make him say the sound twice. So it’s just a slight variation.
03:27
All the rest of the class will be the same. We’re just overriding this one method. So let’s Control + S to save, F5 to run that again. We’ll just copy the instantiation here, and if we’ll say gandhi.speak()
, we get that new message, 'Gandhi says boof, boof'
.
03:50 So that’s extending a parent class. You can override methods from a parent class. All you need to do is subclass and then redefine the method by using the same name.
04:01 All the other methods will work as before, including the constructor. It’s like copying the class and overriding just that one method.
Become a Member to join the conversation.