01 / 21

Abstraction &
Program Design

Master the art of hiding complexity, designing clean interfaces, and building programs that are modular, reusable, and easy to reason about.

AP Computer Science A

What Is Abstraction?

Abstraction is the process of reducing complexity by focusing on the essential features of something while hiding unnecessary details. In computer science, it lets you use a tool without knowing exactly how it works internally.

Simplify
Manage complexity by exposing only what matters
Reuse
Write once, call anywhere without re-reading internals
Collaborate
Teams agree on interfaces, then work independently

Two Kinds of Abstraction

Procedural Abstraction
Wrapping a sequence of steps into a method. The caller knows what the method does, not how it does it.
Data Abstraction
Grouping related data and behavior into a class. Users interact with objects through public methods, not internal fields.

Why Abstraction Matters

Without Abstraction Everything depends on everything With Abstraction Public Interface Methods (Logic) Private Data Clean layers, clear boundaries

Without abstraction, every piece of code can touch every other piece: a tangled mess. With abstraction, code is organized into layers with well-defined boundaries.

The Abstraction Principle

Interface What you can do (public methods) Implementation How it works (private details) Client A Client B Implementation is hidden

Clients interact only through the interface. The implementation can change without breaking any client code.

Interface vs Implementation

Interface Implementation
Visibility Public Private
Answers What does it do? How does it do it?
Who sees it Any client / caller Only the class itself
Changes Rarely (breaks clients) Freely (hidden)
Java keyword public private

One public counting interface, two hidden mechanisms

Mechanical and electronic counting instruments have matching front controls and different interiors.Two implementations can provide the same public counting behavior while storing their internal state differently. A representation change must preserve the interface’s promised behavior.

Preserve public behavior across implementations

Two Counter implementations trace public outputs 0, 1, 2 and 3 while storing opposite internal values.Starting at zero, one Counter stores count and increments it; another stores negativeCount and decrements it, returning its negation. Three increment calls make both getCount results 3. Their private representations differ while the public trace agrees.

Procedural Abstraction: Methods

A method is the primary unit of procedural abstraction in Java. It takes inputs (parameters), performs a task, and optionally returns an output.

public static double circleArea(double radius) { return Math.PI * radius * radius; }
Caller sees
Name: circleArea
Input: radius (double)
Output: area (double)
Caller ignores
That the formula is \(\pi r^2\). Could be a lookup table internally: the caller doesn't care.

Data Abstraction: Classes

A class bundles related data (instance variables) and behavior (methods) into a single unit. The internal state is kept private; the world interacts through public methods.

public class BankAccount { private double balance; // hidden data public BankAccount(double initial) { balance = initial; } public void deposit(double amt) { if (amt > 0) balance += amt; } public double getBalance() { return balance; } }

Two collaborators work on one shared model

Two collaborators adjust one blue articulated model while a second model stands apart.Two variables can provide access to one shared object. Copying a reference creates another access path; it does not duplicate the object.

A new container can share its elements

Two separate list containers point from corresponding slots to the same mutable Box object.Copying the list creates a separate container while copying its element references. Both lists can still reach the same mutable Box. String and Integer values are immutable, so those elements cannot be mutated through either list.

How to Design a Class

1
Identify the concept. What real-world entity or idea does this class represent?
2
Choose private data. What information does each object need to store? Make these private instance variables.
3
Define the public interface. What operations should clients be able to perform? Write constructors, accessors, and mutators.
4
Implement the methods. Write the internal logic. Validate inputs, update state, return results.
5
Test from the client's perspective. Can a user do everything they need through the public methods alone?

Example: Student Class

public class Student { private String name; private int totalPoints; private int numAssignments; public Student(String name) { this.name = name; totalPoints = 0; numAssignments = 0; } public void addGrade(int points) { totalPoints += points; numAssignments++; } public double getAverage() { if (numAssignments == 0) return 0; return (double) totalPoints / numAssignments; } public String getName() { return name; } }

A separate Student interface

The release Student example has a name, id, scores list and named public methods. It is separate from the deck’s Student class with totalPoints and numAssignments.

Two instruments illustrate public operation and private implementation. The Student companion names the public methods and private fields.
Two instruments illustrate public operation and private implementation. The Student companion names the public methods and private fields.
The exact public and private Student members.
The exact public and private Student members.

Client Code: Using Student

// Client code: does NOT see private fields Student s = new Student("Alice"); s.addGrade(90); s.addGrade(80); s.addGrade(95); System.out.println(s.getName() + ": " + s.getAverage()); // Output: Alice: 88.33333333333333
Client Code calls public methods Public: addGrade(), getAverage() getName() Private: name, totalPoints numAssignments Client cannot reach private fields

Method Decomposition

Method decomposition breaks a complex task into smaller helper methods. Each method does one thing well: this is procedural abstraction in action.

// Bad: one giant method public void processOrder() { // 50 lines of validation, pricing, tax, shipping... } // Good: decomposed into clear steps public void processOrder() { validateItems(); double subtotal = calculateSubtotal(); double tax = calculateTax(subtotal); double shipping = calculateShipping(); finalizeOrder(subtotal + tax + shipping); }

Levels of Abstraction

High-Level: Program "Run the game" Classes Player, Board, Piece Classes GameLogic, Display Methods move(), attack() Methods getCell(), isValid() Methods checkWin(), render() Low-Level: Individual Statements if, for, assignments, arithmetic

Parameters and Return Values

Parameters are the inputs to a method: they let you generalize behavior. Return values are the outputs: they let the caller use the result without knowing how it was computed.

// Without parameters: rigid, non-reusable public void greetAlice() { System.out.println("Hello, Alice!"); } public void greetBob() { System.out.println("Hello, Bob!"); } // With parameters: abstract and reusable public void greet(String name) { System.out.println("Hello, " + name + "!"); }

Common Mistakes

✕ Public Instance Variables
Declaring fields as public instead of private destroys data abstraction. Clients can modify state without validation.
✕ God Method
Putting all logic in one massive method. Fails to decompose: hard to read, test, and reuse individual pieces.
✕ Leaking Implementation
Returning a reference to a private mutable object (like an ArrayList). Clients can modify your internal state directly.

Summary of Key Concepts

Concept What It Means Java Mechanism
Procedural Abstraction Hide how a task is done Methods
Data Abstraction Hide internal state Classes with private fields
Encapsulation Bundle data + behavior Private vars + public methods
Decomposition Break complex into simple Helper methods
Interface Public contract Public method signatures

Question 1 of 5

Which of the following best describes the purpose of abstraction in program design?

Question 2 of 5

A client writes Counter c = new Counter();. Which statement would cause a compile-time error?

Question 3 of 5

Breaking a large method into several smaller helper methods is an example of which design principle?

Question 4 of 5

A class has a private ArrayList<String> items field and a public method getItems() that returns items directly. What is the problem?

Question 5 of 5

Which pair correctly matches the interface and implementation sides of abstraction?