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
Override a method
easy10 minReplace the parent's speak with the child's own.
- 2
Use polymorphism
easy10 minCall speak on three different animals and join the answers.
- 3
Extend rather than replace
easy10 minAdd to the parent's answer instead of rewriting it.
- 4
Choose the method at runtime
easy10 minOne function, two different objects, two different answers.
- 5
Override with the same shape
medium10 minBoth formatters take a string and return a string.
Find the bug(5)
- 1
Fix the override with a different name
easy8 minAn override must use the same method name.
- 2
Fix the parent's work discarded
easy8 minThe child should build on the parent's answer.
- 3
Fix the override on the parent
easy8 minThe new behaviour was put on the parent, so every animal barks.
- 4
Fix the missing super in the constructor
easy8 minAn overriding constructor still has to call super.
- 5
Fix the override changing the shape
easy8 minThe 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