Courses / Computer Science I
Programming II

Abstract Data Use

Computer Science I 241 words Free to read

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.

ConceptDefinition
ADTDefined by operations, not internals
BarrierLine 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.
An abstract data type: one interface, two insides updating in lockstep

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.

ModifierAccess Level
privateReachable only inside the class
publicMethods 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.

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

12practice questions
2interactive scenes

Programming II