DEV Community

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

Posted on

Encapsulation, as applied to an RPG

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


Youโ€™re making an RPG.

You have a Character (class) who has health and armor (private fields).

Among other things, a Character can heal() or takeDamage() (public methods).

heal() increases health.

takeDamage() decreases health based on armor.

Other objects are not able to modify the health of a Character directly, rather, they can only call the methods exposed by the class.

This is encapsulation.

Cover image was created by Buch.

Top comments (0)