DEV Community

Cover image for Inheritance, as applied to an RPG
Nathan Englert
Nathan Englert

Posted on

Inheritance, as applied to an RPG

This is the second article in a series I'm doing called DevBits. These are mini-articles that show how object-oriented programming concepts can be applied.


You’re making an RPG.

You have a Character (class) who has health (field).

A Character can move() or attack() (methods).

A Player (child class) is a type of Character, but also has experience (field).

An Enemy (child class) is a type of Character, but also has loot (field).

Player and Enemy both share the same fields and methods from Character, but can have any additional fields of their own.

This is inheritance.

Cover image was created by Buch.

Top comments (0)