concept oop Encapsulation is the bundling of data (fields) and the methods that operate on that data into a single unit (Class/Struct), while restricting direct access to some of the object’s components.

  • Goal: Protect the internal state of an object from corruption.
  • Mechanism: Access Modifiers (public, private, protected).

The “Black Box” Metaphor

You drive a car using the Public Interface (Steering Wheel, Pedals). You do not directly interact with the Private Implementation (Fuel Injection, Spark Plugs). This allows the engineer to change the engine without forcing you to relearn how to drive.


Examples: Bank Account

We want to prevent direct access to balance so users can’t set it to negative values. They must use deposit() and withdraw().


Systems Context

  • C: C has no true encapsulation. Everything in a header is “public.” To simulate “private,” C developers use Opaque Pointers (Forward declaring a struct in .h but defining it in .c).
  • Rust: Encapsulation is enforced at the Module level (mod), not just the Class level.