Replace the parent's speak with the child's own.
Acceptance criteria
- 1.#out should show woof.
JavaScript Tutorial · JavaScript Classes
These 10 tasks go with the JavaScript Method Overriding chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “A child class can replace a method it inherited with its own version.” .
1.Override a methodexercise · easy · 10 min
Replace the parent's speak with the child's own.
Passes when: #out should show woof.
2.Use polymorphismexercise · easy · 10 min · practice plan
Call speak on three different animals and join the answers.
Passes when: #out should show woof meow some sound.
3.Extend rather than replaceexercise · easy · 10 min · practice plan
Add to the parent's answer instead of rewriting it.
Passes when: #out should show an animal, specifically a dog.
4.Choose the method at runtimeexercise · easy · 10 min · practice plan
One function, two different objects, two different answers.
Passes when: #out should show woof generic.
5.Override with the same shapeexercise · medium · 10 min · practice plan
Both formatters take a string and return a string.
Passes when: #out should show HELLO.
6.Fix the override with a different namebug fix · easy · 8 min
An override must use the same method name.
Passes when: #out should show woof.
7.Fix the parent's work discardedbug fix · easy · 8 min · practice plan
The child should build on the parent's answer.
Passes when: #out should show an animal, specifically a dog.
8.Fix the override on the parentbug fix · easy · 8 min · practice plan
The new behaviour was put on the parent, so every animal barks.
Passes when: #out should show woof generic.
9.Fix the missing super in the constructorbug fix · easy · 8 min · practice plan
An overriding constructor still has to call super.
Passes when: #out should show square has 4 sides.
10.Fix the override changing the shapebug fix · easy · 8 min · practice plan
The override returns a different kind of value, so it is no longer interchangeable.
Passes when: #out should show HELLO.
Between them these tasks cover Overriding, Polymorphism and Runtime dispatch.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.