Back to Encapsulation
Encapsulation Builder

Protect the Cave Map

Keep the map private inside an Explorer Backpack. Use methods as the only safe way to pack or read it.

MissionPack the mapRead it safelyBlock direct access
Builder idea: #map is protected data. packMap() and readMap() are trusted doors that control it.
1. Build the controls

Choose a method, then run it.

class ExplorerBackpack {
  #map = "treasure map";

  packMap() { /* protect it */ }
  readMap() { /* safely use it */ }
}
Pack map Read map Block shortcut
2. Watch the object

The backpack controls its own map.

Explorer backpack without its map
An open treasure map
Put the map in the bag.

Complete all three tests to prove the map is encapsulated.