Abstract Data Types
A powerful idea in programming is separating what data does from how it does it. An abstract data type (ADT) is defined by its operations and their behavior, not its internal representation. A stack ADT uses push, pop, and peek in a last-in-first-out order, whether backed by an array or linked list.
The abstraction barrier (interface) divides public operations from hidden internals. Information hiding (encapsulation) lets implementations be swapped without changing user code. Program against the interface, not the internal machinery.
| Concept | Definition |
|---|---|
| ADT | Defined by operations, not internals |
| Barrier | Line between public calls and private code |
Pitfall: Reaching through the abstraction barrier. Relying on internal storage breaks code when implementations change. Always use published operations.
Classes and Encapsulation
An ADT becomes concrete via a class, bundling fields (internal data) and methods (operations). An object is an instance of a class. A constructor runs at creation to establish the class invariant—the property that must always hold true.
| Modifier | Access Level |
|---|---|
| private | Reachable only inside the class |
| public | Methods forming the class interface |
Encapsulation makes invariants defensible by marking fields private.
Pitfall: Making fields public or adding getters/setters for all of them. This turns a class into an unguarded bag of variables, letting callers violate the invariant.