Problem overview
The lesson gives you two classes. Dog stores a name in its constructor and has a bark() method that prints Woof! I am <name>. Puppy extends Dog and overrides bark(), but its body is empty. Your task is to fill it in so that a puppy first does everything a dog does and then adds its own line, Yip! Said <name>.
The driver at the bottom creates two puppies and one plain dog. The expected output is:
Woof! I am Buddy
Yip! Said Buddy
Woof! I am Rex
Yip! Said Rex
Woof! I am Max
Each puppy prints two lines, while the plain Dog prints only the parent's line. The keyword that makes this possible is super.
Approach: extend the parent's method, don't replace it
When a subclass defines a method with the same name as one in its parent, the subclass version wins for objects of the subclass. That is called overriding, and by default it replaces the parent's behaviour completely. With an empty bark() in Puppy, a puppy would print nothing at all.
Often you want to keep the parent's behaviour and add to it. Inside a method, super.bark() calls the parent's version of bark. The call runs on the same object, so this is still the puppy and this.name still holds the name that Dog's constructor stored.
The alternative is to copy the parent's console.log line into Puppy. That works until someone changes the line in Dog: the puppy would then keep the old text. With super.bark(), the puppy follows the parent automatically.
Here is what happens when the driver runs buddy.bark(). JavaScript finds bark on Puppy first, because buddy was made by new Puppy. That method starts with super.bark(), which jumps to Dog's bark and prints Woof! I am Buddy. When it finishes, control returns to Puppy's method, which prints Yip! Said Buddy. Then rex.bark() repeats the same two steps with its own name.
Step by step
- Call the parent's version first. Write
super.bark();as the first statement ofPuppy'sbark(). This produces theWoof!line. - Then add the puppy's own output. Log
"Yip! Said "followed bythis.name. The order matters for the expected output: the parent's line has to come first.
That is the whole change. Puppy needs no constructor of its own. When a derived class has no constructor, JavaScript supplies one that passes all its arguments to the parent's constructor, so new Puppy("Buddy") still sets this.name.
The solution in JavaScript
class Dog {
constructor(name) { this.name = name; }
bark() {
console.log("Woof! I am " + this.name);
}
}
class Puppy extends Dog {
bark() {
super.bark();
console.log("Yip! Said " + this.name);
}
}
let buddy = new Puppy("Buddy");
buddy.bark();
let rex = new Puppy("Rex");
rex.bark();
let dog = new Dog("Max");
dog.bark();
Details worth knowing about super:
super.method()only works inside methods. It looks the method up on the parent class's prototype, but calls it with the currentthis. That is why the parent's line prints the puppy's name.- The order is your choice. Calling
super.bark()last gives the lines in the opposite order. For aPuppynamed Pip that class would printYip! Said Pipand thenWoof! I am Pip. The lesson's expected output needs the parent first. - Plain
Dogobjects are unaffected. Overriding changes onlyPuppy. Thedogin the driver still usesDog's ownbark. super(...)is a different use of the same word. In a constructor of a derived class,super(name)calls the parent's constructor. It must run before the constructor touchesthis. A constructor that skips it and writesthis.name = namethrowsReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor. This lesson needs only the method form.- It reaches the nearest parent. With a chain
Dog,Puppy, then a subclass ofPuppythat has nobarkof its own, callingbark()runsPuppy's version, which in turn callsDog's. Both lines print.
Edge cases and common mistakes
- Forgetting
super.bark(). The method then replaces the parent's behaviour, and each puppy prints only theYip!line. TheWoof!lines for Buddy and Rex are missing. - Writing
super()instead ofsuper.bark(). Outside a constructor, a baresuper()call is a syntax error. To call a method of the parent, usesuper.followed by the method name. - Hard-coding the name. The driver builds two puppies with different names, so the line has to use
this.name. - Calling
this.bark()insidebark. That callsPuppy's own method again, which calls itself again, and so on until the call stack overflows.super.bark()is what reachesDog's method. - Detaching the method. Taking
Puppy.prototype.barkout and calling it as a plain function leavesthisundefined, so readingthis.namethrows aTypeError. Call methods on an object, asbuddy.bark()does. - Extra spaces in the text. The string is
"Yip! Said "with one trailing space, followed by the name. A missing space givesYip! Saidglued to the name.
Testing
Try it in the editor above. Besides the driver, these checks are worth running:
- A
Puppynamed"Pip"printsWoof! I am Pipand thenYip! Said Pip. - A
Dognamed"Pip"prints onlyWoof! I am Pip. - With
super.bark()removed, aPuppynamed"Pip"prints onlyYip! Said Pip. - A subclass of
Puppywith an empty body, created with the name"Kit", printsWoof! I am Kitand thenYip! Said Kit, because it inheritsPuppy'sbarkand so gets both lines.
