OOP Studio Ages 9-15
Polymorphism Challenge
Create at least two child classes that use the same method name in different ways.
Create your own shared method.
Example code
class Power {
constructor(name) {
this.name = name;
}
use() {
say(this.name + " is used.");
}
}
class SpeedPower extends Power {
use() {
say(this.name + " makes the hero zoom!");
}
}
class ShieldPower extends Power {
use() {
say(this.name + " blocks the next hit!");
}
}
let powers = [
new SpeedPower("Lightning Boots"),
new ShieldPower("Bubble Shield")
];
for (let power of powers) {
power.use();
}
Program result
- Run your code to see output.
Type real JavaScript, then run it.
Polymorphism challenge complete.
You used one shared method name so different objects could do their own version of an action.
Next Lesson