Problem overview
The lesson gives you two classes. Dog has a protected name and a bark() method that prints Woof! I am <name>. Puppy extends Dog and overrides bark(), but the body of the override is empty. Your job is to fill it so that a puppy first does everything a dog does, then adds its own line: Yip! Said <name>.
The driver creates two puppies, Buddy and Rex, and one plain dog, Max. A puppy prints two lines when it barks and the dog prints one, so the expected output is five lines: the two lines for Buddy, the two for Rex, then Woof! I am Max.
Approach: extend the parent's method instead of replacing it
When a subclass declares a method with the same name as one in its parent, the subclass version overrides it. Calling bark() on a Puppy runs the puppy's code and the dog's code is not involved at all. That is often what you want, but sometimes the subclass only wants to add to the parent's behavior.
The keyword super gives access to the parent class from inside a method. super.bark() runs Dog's bark on the same object, so this.name inside it is still the puppy's name. Nothing is copied: you are calling the parent's code with the current object as this.
The order matters. Calling super.bark() first and then printing your own line makes the parent's output come first. Writing them the other way round gives the opposite order. The lesson asks for the parent's line first.
Step by step
- Call
super.bark()as the first statement ofPuppy.bark. This keeps the dog's behavior: without it the override would replace the parent's line instead of adding to it. - Log the puppy's own line by joining
"Yip! Said "andthis.name.nameisprotectedinDog, which means a subclass can read it throughthis, so the puppy needs no constructor and no getter of its own.
That is the whole change. Puppy has no constructor, so it uses Dog's one: new Puppy("Buddy") runs Dog's constructor, and the parameter property protected name: string creates and fills the field.
The solution in TypeScript
class Dog {
constructor(protected name: string) {}
bark(): void {
console.log("Woof! I am " + this.name);
}
}
class Puppy extends Dog {
bark(): void {
super.bark();
console.log("Yip! Said " + this.name);
}
}
const buddy = new Puppy("Buddy");
buddy.bark();
const rex = new Puppy("Rex");
rex.bark();
const dog = new Dog("Max");
dog.bark();
Running it prints:
Woof! I am Buddy
Yip! Said Buddy
Woof! I am Rex
Yip! Said Rex
Woof! I am Max
Details worth knowing
- Parameter properties. Writing
protected name: stringin the constructor's parameter list declares a field and assigns it in one go. It is shorthand for declaringprotected name: string;in the class body and writingthis.name = name;in the constructor. protectedagainstprivate. Aprivatemember can be used only inside its own class. Aprotectedmember can also be used inside subclasses, which is whyPuppycan readthis.name. Outside both classes neither is accessible: readingbuddy.namein the driver makes the compiler reportProperty 'name' is protected and only accessible within class 'Dog' and its subclasses.- The check is only in the compiler.
protectedandprivateare erased when TypeScript compiles to JavaScript. Even with that error reported, the compiled program still runs, andconsole.log(buddy.name)printsBuddy. If you need a field that is private at run time, JavaScript's#namesyntax does that. - The optional
overridemodifier. Writingoverride bark(): voidtells the compiler that the parent must have a method with that name. If you misspell it, for example asoverride bork(), the compiler reportsThis member cannot have an 'override' modifier because it is not declared in the base class 'Dog'. The lesson's code works without the modifier, and with it the output is the same. - Dynamic dispatch. Which
barkruns depends on the object, not on the variable's declared type. Withconst d: Dog = new Puppy("Rex"), callingd.bark()prints both lines, because the object is aPuppy. super.method()works in methods. In a constructor of a subclass that declares one, you call the parent's constructor withsuper(...)before usingthis. This lesson doesn't need that becausePuppyhas no constructor.
Edge cases and common mistakes
- Forgetting the
super.bark()call. The override then replaces the parent's behavior. A puppy named Buddy prints onlyYip! Said Buddy, and theWoof!line is lost. - Swapping the two statements. If the puppy's line comes first and
super.bark()second, the program still runs, but the output for Buddy isYip! Said Buddyand thenWoof! I am Buddy. The lesson's tests expect the parent's line first. - Hard-coding a name. The line must use
this.name, because Buddy and Rex are two objects of the same class and the name comes from the object. - Writing
super.name. Instance fields live on the object, not on the parent class, sothis.nameis the way to read them.superis for calling the parent's methods. Inside a puppy's method,console.log(super.name)printsundefinedwhilethis.nameprintsBuddy, because the field was never stored on the parent class itself. - Expecting
Dogto change. Overriding inPuppydoesn't touchDog. The plain dog Max still prints only the parent's line, with noYip!.
Testing
new Puppy("Buddy").bark()printsWoof! I am Buddyand thenYip! Said Buddy.new Puppy("Rex").bark()prints the same two lines with Rex's name.new Dog("Max").bark()prints onlyWoof! I am Max.
Press Run in the editor above to see all five lines together.
