Start learning free

LESSON 38 OF 70 · OBJECT-ORIENTED PROGRAMMING

Calling the Parent with super in TypeScript

Practice Read the full explanation

TL ; DR:

Inside a method of a class that extends another, super.bark() calls the parent's version of bark on the same object. This lets you extend the parent's behavior instead of replacing it.


class Puppy extends Dog {
    bark(): void {
        super.bark();   // do everything the parent did
        console.log("Yip! Said " + this.name);
    }
}

Why it matters

Overriding a method replaces the parent's behavior. Often you want to extend it instead: keep what the parent did and add something more. A typical example is a save method: the parent writes the row to the database, and a subclass runs the parent's save first and then writes a log line.

Your task

The Puppy class already overrides bark(): void, but the body is empty. Inside it:

  1. call the parent's version with super.bark();, so Woof! I am <name> still prints;
  2. then console.log("Yip! Said " + this.name);.

Notes:

  • Dog's constructor declares protected name: string, so Puppy may read this.name but code outside the classes may not.
  • Puppy has no constructor of its own, so it inherits Dog's: new Puppy("Buddy") sets the name.

The driver (already written, it runs when you press Run):

const buddy = new Puppy("Buddy");
buddy.bark();

const rex = new Puppy("Rex");
rex.bark();
const dog = new Dog("Max");
dog.bark();
  • buddy is a Puppy: its bark prints the parent's line, then its own.
  • rex is another Puppy: the names come from the object, nothing is hard-coded.
  • dog is a plain Dog: its bark is unchanged and prints only the parent's line, with no Yip!.

Expected output:

Woof! I am Buddy
Yip! Said Buddy
Woof! I am Rex
Yip! Said Rex
Woof! I am Max
STEP 1 OF 2
    Ctrl + Enter to check

    TYPESCRIPT · OBJECT-ORIENTED PROGRAMMING

    Base Keyword in TypeScript, explained

    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

    1. Call super.bark() as the first statement of Puppy.bark. This keeps the dog's behavior: without it the override would replace the parent's line instead of adding to it.
    2. Log the puppy's own line by joining "Yip! Said " and this.name. name is protected in Dog, which means a subclass can read it through this, 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: string in the constructor's parameter list declares a field and assigns it in one go. It is shorthand for declaring protected name: string; in the class body and writing this.name = name; in the constructor.
    • protected against private. A private member can be used only inside its own class. A protected member can also be used inside subclasses, which is why Puppy can read this.name. Outside both classes neither is accessible: reading buddy.name in the driver makes the compiler report Property 'name' is protected and only accessible within class 'Dog' and its subclasses.
    • The check is only in the compiler. protected and private are erased when TypeScript compiles to JavaScript. Even with that error reported, the compiled program still runs, and console.log(buddy.name) prints Buddy. If you need a field that is private at run time, JavaScript's #name syntax does that.
    • The optional override modifier. Writing override bark(): void tells the compiler that the parent must have a method with that name. If you misspell it, for example as override bork(), the compiler reports This 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 bark runs depends on the object, not on the variable's declared type. With const d: Dog = new Puppy("Rex"), calling d.bark() prints both lines, because the object is a Puppy.
    • super.method() works in methods. In a constructor of a subclass that declares one, you call the parent's constructor with super(...) before using this. This lesson doesn't need that because Puppy has 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 only Yip! Said Buddy, and the Woof! 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 is Yip! Said Buddy and then Woof! 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, so this.name is the way to read them. super is for calling the parent's methods. Inside a puppy's method, console.log(super.name) prints undefined while this.name prints Buddy, because the field was never stored on the parent class itself.
    • Expecting Dog to change. Overriding in Puppy doesn't touch Dog. The plain dog Max still prints only the parent's line, with no Yip!.

    Testing

    • new Puppy("Buddy").bark() prints Woof! I am Buddy and then Yip! Said Buddy.
    • new Puppy("Rex").bark() prints the same two lines with Rex's name.
    • new Dog("Max").bark() prints only Woof! I am Max.

    Press Run in the editor above to see all five lines together.

    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free