liveBook · Manning

Encapsulation

As explained in Grokking Data Structures

Encapsulation is the bundling of data with the operations that manage it while restricting direct access to the data's internal representation.

Encapsulation bundles data with the operations that manage it, so callers do not directly edit internal state. Instead of keeping an array’s size in a shared variable, an array object stores and updates its own size. section 2.10

A class provides the boundary: methods validate and change the data, while internal attributes hold details such as capacity and current size. This prevents callers from accidentally desynchronizing related values. section 2.10

Encapsulation becomes especially valuable when a data structure must preserve an invariant:

  • An unsorted array keeps its size with the instance and updates it through array operations. section 2.10
  • A sorted array restricts changes to insert and delete methods, protecting the requirement that entries remain sorted. section 3.3
  • Composition places a core array inside the higher-level class, rather than inheriting from it; this offers more design flexibility. section 2.10

An encapsulated array

UnsortedArray stores the underlying array, maximum capacity, and current size as instance attributes. Clients use the class operations instead of maintaining the size separately. section 2.10

class UnsortedArray:
    def __init__(self, max_size, typecode = 'l'):
        self._array = Array(max_size, typecode)
        self._max_size = max_size
        self._size = 0

The constructor shows encapsulation through bundled state and composition: the wrapper owns its size while delegating storage to an internal Array. section 2.10

How Grokking Data Structures covers it

How deep it goes
Explains shared-state problems, class boundaries, composition, and invariant protection, but notes Python lacks true private class attributes.
Examples
Uses UnsortedArray and SortedArray constructors, size tracking, composition, and insert/delete operations as concrete examples.
What it assumes
Assumes basic arrays and operations, while introducing classes, encapsulation, composition, and inheritance through implementation examples.
What you can do after
Build it from scratch using Python classes that bundle state, delegate storage, and expose controlled operations.
How it connects
Connects classes, data structures, composition, inheritance, invariants, and sorted arrays.

Also explained in

Compare how 14 books explain encapsulation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Data Structures. The section links point to the parts of the book it draws on.