🏛️ Object-Oriented Programming

Inheritance (Basic)

Project Evidence Required
Create class hierarchy with 2+ levels (e.g. GameObject → Character → Player).
Assessment Method
Code review: extends keyword, inheritance chain

What it is

Inheritance lets one class extend another using the extends keyword. The child class automatically gets the parent’s properties and methods, and can add new ones or override existing ones. Multi-level inheritance chains the relationship — a grandchild inherits from its parent, which inherits from its grandparent.

Why it matters

Inheritance is code reuse at the architecture level. Position, velocity, sprite rendering, and physics belong on a base class because every entity needs them. Without inheritance, you’d duplicate that boilerplate in every single class.

How GateGame implements it

My class hierarchy goes three levels deep. GameLevelMazeSub extends GameLevel for the level itself. Inside the level, the entity chain is GameObject → Character → Player (and similarly for Npc). The engine’s GameObject handles position and the abstract draw lifecycle; Character adds animation frames and physics defaults; Player adds keyboard input and score tracking on top. Three levels comfortably exceeds the 2+ requirement.

Code Example

// Level inherits from the engine's GameLevel
class GameLevelMazeSub extends GameLevel {
  constructor(gameEnv) {
    super(gameEnv);
    // ... instantiate this.classes
  }
}

// Entity chain (provided by the engine):
// GameObject → Character → Player

Key Takeaway

A 3-level chain (GameObject → Character → Player) means the physics code lives in exactly one place — and every entity gets it for free.