2.5.1 Introduction to Encapsulation

Video: Encapsulation (7:10)
Encapsulation Controls How Object State Is Accessed

Earlier in the course, many examples used public fields so you could focus on objects, references, methods, and program flow.

As classes become more important to the design of a program, unrestricted field access can become a problem.

Encapsulation is the idea of keeping a class's internal state under the control of the class.

Public Fields Allow Direct Access

Consider:

public class Player
{
    public int jerseyNumber;
}

Code outside the Player class can directly change the field:

player1.jerseyNumber = 7;

That is simple, but it also means outside code can assign any value that the field type accepts.

Direct Access Can Bypass the Class's Rules

Suppose the program requires a jersey number to be greater than zero.

With a public field, outside code can still write:

player1.jerseyNumber = -5;

The assignment is type-compatible because -5 is an int.

But it may violate the rules of the program.

This reveals an important distinction:

valid C# value
       ≠
valid object state
The Class Should Protect Its Own State

An object should not have to depend on every caller remembering all of its rules.

Instead, the class can control which state is directly accessible and which changes must go through class behavior.

Conceptually:

Object diagram showing outside code, then controlled class behavior, then object state.
OutsideCode.
  • New
  • Changed
  • Removed

This creates a boundary around the internal representation of the object.

Encapsulation Separates What a Class Exposes from How It Stores State

A useful way to think about a class is as having two views.

Outside view:

Encapsulation helps keep those two concerns separate.

A Soccer Example

Imagine a Player object that tracks whether the player is available for a match.

If every part of the program can directly change every field, the Player class has little control over its own state.

A safer design may restrict direct access and provide a class-defined way to update the state.

The exact implementation can vary, but the design question is the same:

Encapsulation Reduces Coupling

When outside code reaches directly into many fields, that outside code becomes tightly connected to the class's internal structure.

If the class later changes how it stores information, many callers may also need to change.

Encapsulation reduces that dependency by encouraging callers to work through the class's intended public surface rather than relying on internal details.

Encapsulation Is Not About Hiding Everything

The goal is not to make a class impossible to use.

A class still needs to expose the behavior or information that other parts of the program legitimately require.

The design question is:

The next page introduces the public and private access modifiers that C# uses to express that decision.

Encapsulation Supports Safer Objects

When a class controls access to its state, it becomes easier to:

  • prevent unintended direct changes;
  • keep object state consistent;
  • centralize rules inside the class that owns the data;
  • change internal implementation without forcing every caller to change;
  • make class responsibilities clearer.
The Core Idea

Encapsulation means treating an object as more than a collection of publicly writable fields.

The class owns its state and should control how that state is exposed or changed.

Activity diagram showing outside code, then publicly allowed access, then class-controlled state.
OutsideCode.
  • New
  • Changed
  • Removed

The pages that follow show how access modifiers and class methods help create that boundary.