Start learning free

LESSON 38 OF 69 · OBJECT-ORIENTED PROGRAMMING

Base Keyword in JavaScript: Calling the Parent with super

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() {
        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, but the body is empty. Inside Puppy's bark():

  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:

  • super.bark() works inside a class method; super by itself refers to the parent class (Dog).
  • this.name was set by Dog's constructor; Puppy has no constructor of its own, so it inherits it.

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

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

let rex = new Puppy("Rex");
rex.bark();
let 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

    JAVASCRIPT · OBJECT-ORIENTED PROGRAMMING

    Base Keyword in JavaScript, explained

    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

    1. Call the parent's version first. Write super.bark(); as the first statement of Puppy's bark(). This produces the Woof! line.
    2. Then add the puppy's own output. Log "Yip! Said " followed by this.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 current this. 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 a Puppy named Pip that class would print Yip! Said Pip and then Woof! I am Pip. The lesson's expected output needs the parent first.
    • Plain Dog objects are unaffected. Overriding changes only Puppy. The dog in the driver still uses Dog's own bark.
    • 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 touches this. A constructor that skips it and writes this.name = name throws ReferenceError: 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 of Puppy that has no bark of its own, calling bark() runs Puppy's version, which in turn calls Dog'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 the Yip! line. The Woof! lines for Buddy and Rex are missing.
    • Writing super() instead of super.bark(). Outside a constructor, a bare super() call is a syntax error. To call a method of the parent, use super. 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() inside bark. That calls Puppy's own method again, which calls itself again, and so on until the call stack overflows. super.bark() is what reaches Dog's method.
    • Detaching the method. Taking Puppy.prototype.bark out and calling it as a plain function leaves this undefined, so reading this.name throws a TypeError. Call methods on an object, as buddy.bark() does.
    • Extra spaces in the text. The string is "Yip! Said " with one trailing space, followed by the name. A missing space gives Yip! Said glued to the name.

    Testing

    Try it in the editor above. Besides the driver, these checks are worth running:

    • A Puppy named "Pip" prints Woof! I am Pip and then Yip! Said Pip.
    • A Dog named "Pip" prints only Woof! I am Pip.
    • With super.bark() removed, a Puppy named "Pip" prints only Yip! Said Pip.
    • A subclass of Puppy with an empty body, created with the name "Kit", prints Woof! I am Kit and then Yip! Said Kit, because it inherits Puppy's bark and so gets both lines.
    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free