DEV Community

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

Posted on

3

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.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post