Skip to main content

JavaScript Method Overriding — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Method Overriding lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    Override a method

    easy10 min

    Replace the parent's speak with the child's own.

  2. 2

    Use polymorphism

    easy10 min

    Call speak on three different animals and join the answers.

  3. 3

    Extend rather than replace

    easy10 min

    Add to the parent's answer instead of rewriting it.

  4. 4

    Choose the method at runtime

    easy10 min

    One function, two different objects, two different answers.

  5. 5

    Override with the same shape

    medium10 min

    Both formatters take a string and return a string.

Find the bug(5)

  1. 1

    Fix the override with a different name

    easy8 min

    An override must use the same method name.

  2. 2

    Fix the parent's work discarded

    easy8 min

    The child should build on the parent's answer.

  3. 3

    Fix the override on the parent

    easy8 min

    The new behaviour was put on the parent, so every animal barks.

  4. 4

    Fix the missing super in the constructor

    easy8 min

    An overriding constructor still has to call super.

  5. 5

    Fix the override changing the shape

    easy8 min

    The override returns a different kind of value, so it is no longer interchangeable.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace