Method Overriding
Object-Oriented Programming · Override parent methods (update(), draw(), handleCollision()).
Method Overriding
What it is
Method overriding happens when a child class defines a method with the same name as one in the parent class. The child’s version replaces the parent’s — but the child can still invoke the parent’s version via super.methodName() if it wants to augment rather than replace.
Why it matters
Overriding is how subclasses customize behavior without rewriting everything. The base update() handles generic physics, but the Player.update() override adds keyboard reading on top. Same method name, different behavior per class — that’s polymorphism, and it’s what makes object-oriented design powerful.
How GateGame implements it
In Maze of Shadows, the Player overrides update() to read WASD input before delegating to the base physics. The NPC classes override interact() to trigger their own dialogue trees — Exit Warden offers a level transition, Shadow NPC offers maze hints. Each draw() override handles sprite-specific rendering like horizontal flipping based on facing direction.
Code Example
// Exit Warden overrides interact() with its own dialogue logic
interact: function() {
this.dialogueSystem.showDialogue(
"You followed the path all the way here. Are you ready to move on?",
"Exit Warden",
this.spriteData.src
);
this.dialogueSystem.addButtons([
{ text: "Step Through", primary: true, action: () => { /* transition */ } },
{ text: "Not yet", action: () => this.dialogueSystem.closeDialogue() }
]);
}
Key Takeaway
Same method name, different behavior per subclass. Polymorphism is how one engine handles many entity types without an
if/elsechain.