Problem overview
The lesson has two classes. Dog stores a name in __init__ and has a bark method that prints Woof! I am followed by the name. Puppy is a subclass of Dog with its own bark, whose body is empty. Your job is to fill it in so a puppy first does everything a dog does when it barks, and then prints one more line, Yip! Said followed by its name.
The driver creates two puppies, Buddy and Rex, and a plain dog, Max. A puppy's bark prints two lines, and Max's prints only the first one.
Extending a method instead of replacing it
When a subclass defines a method with the same name as one in its parent, the subclass version replaces the parent's for objects of the subclass. That is called overriding. Often replacing is too much: you want to keep what the parent did and add something. For that, the override calls the parent's version itself.
In Python, super() does this. It returns a stand-in for the parent class, so super().bark() runs Dog.bark on the current object. Inside it, self is still the puppy, which is why self.name works: Puppy has no __init__ of its own, so it inherits the one from Dog, and that one stored the name.
Think of an override that calls super() as a sandwich. Code before the call runs first, the parent's version runs in the middle, and code after the call runs last. Which side you put your own code on decides the output order, and this lesson asks for the parent's line first.
What super() looks up
Every class knows its parent, and super() uses that to find the next class up the chain that has the method you ask for. Here the chain is Puppy, then Dog, then object, the class every Python class ends with. So super().bark() inside Puppy lands on Dog.bark. The call runs on the same object: no second dog is created, and self.name is the name that was given when the puppy was made.
The same tool works in constructors. A subclass that needs an extra attribute can write its own __init__, call super().__init__(name) so the parent still sets self.name, and then add its own fields. This lesson doesn't need that, because Puppy only changes bark.
Step by step
- Call the parent's
barkfirst. Writesuper().bark()with both pairs of parentheses.super()is a call that produces the parent stand-in, and.bark()is a call on it. Without this line the puppy would never print the Woof line. - Add the puppy's own line. Print
"Yip! Said "joined withself.name. Because it comes after the parent call, the output order is the parent's line and then the puppy's.
The solution in Python
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
print("Woof! I am " + self.name)
class Puppy(Dog):
def bark(self):
super().bark()
print("Yip! Said " + self.name)
buddy = Puppy("Buddy")
buddy.bark()
rex = Puppy("Rex")
rex.bark()
dog = Dog("Max")
dog.bark()
Notice that the driver at the bottom is part of the lesson's file. You only write the two lines inside Puppy.bark.
What the output shows
The expected output is five lines:
Woof! I am Buddy
Yip! Said Buddy
Woof! I am Rex
Yip! Said Rex
Woof! I am Max
The names come from the objects, nothing is hard-coded in the class. Max is a Dog, so his bark is the original and never prints a Yip! line.
Edge cases and common mistakes
- Forgetting the parentheses.
super.bark()without()raisesAttributeError: type object 'super' has no attribute 'bark', becausesuperis the built-in itself and not the stand-in it returns. Writesuper().bark(). - Passing
selfagain.super().bark(self)raisesTypeError: Dog.bark() takes 1 positional argument but 2 were given:super()already binds the method to the current object, sobarkreceivesselfby itself. - Leaving out the call. If the override only prints its own line, the parent's behavior is replaced, not extended.
- The order of the lines. The parent's version can also run after your own code, or in the middle of it. With this lesson's order, the parent's line prints first. If the print comes before
super().bark(), the same two lines appear the other way round:Yip! Said Pipand thenWoof! I am Pip. - Calling the parent by name.
Dog.bark(self)also works and prints the same line.super()is preferred because it doesn't repeat the parent's name, so it keeps working if the parent class changes.
Trying it yourself
A few checks you can add below the driver in the editor above:
isinstance(Puppy("Zed"), Dog)givesTrue: a puppy is a dog, so it can be used wherever a dog is expected.Dog.bark(Puppy("Zed"))printsWoof! I am Zedonly: it calls the parent's version directly and skips the puppy's override.- Giving
Puppya newbarkthat prints only its own line removes the Woof line from every puppy, while Max keeps his.
Key takeaways
- Overriding replaces a parent's method; calling
super().method()inside the override extends it. - A subclass without its own
__init__uses the parent's, so attributes such asself.nameare there. - Methods of the parent class are unaffected: a plain
Dogstill barks the old way.
