Extend a Method (Solution)
You just want to augment it. You want to extend it with this default argument. First of all, let’s see how we can create a default argument in Python. That is by using an equal sign here and then passing the default value to the argument, I’m going to say this is
That gives me access to the parent class. And then I’m going to call the
.speak() method from the parent class, and I will pass it the
sound. And this, again, is going to be whatever sound you pass in when calling
.speak() on a
GoldenRetriever object. But if you don’t pass in any sound, then it’ll use the default value of
"Bark" that you defined here.
So for example, I’m going to say
buddy.speak() and then pass it
Buddy says Howl. But now if I am going to call
buddy without passing an argument, then it should default to
"Bark" like I defined in the
.speak() method on the
GoldenRetriever class and it’s working:
Buddy says Bark.
and it’s an extension of the parent class method because you’re calling the method in the body of your
.speak() method in the child class. If you wouldn’t call it, then you would effectively override the method from the parent class with a
Become a Member to join the conversation.