Class Design with Abstraction
Deciding what a class knows and what it can do — on paper, before any code exists.
- Abstraction reduces complexity by focusing on the main idea: hide details irrelevant to the question at hand, gather related useful ones.
- Data abstraction separates the abstract properties of a data type from the concrete details of its representation.
- Data can be a single variable or a collection, such as a class or a set of data.
- An attribute is a data abstraction defined in a class outside any method or constructor.
- An instance variable's value is unique to each instance; a class variable is shared by all instances.
- "Outside any method or constructor" is positional — a declaration inside a method is a local variable, not an attribute.
- Procedural abstraction names a process, letting a method be used knowing only what it does, not how.
- Method decomposition breaks larger behaviors into smaller ones by creating a method for each individual smaller behavior.
- A procedural abstraction may extract shared features to generalize functionality instead of duplicating code, allowing code reuse.
- The deliverable is a design in natural language or diagrams listing classes with all attributes and behaviors — not code.
1Designing Before Coding
Every class you write starts as a decision about what to include and what to leave out. That decision has a name.
Abstraction is the process of reducing complexity by focusing on the main idea. By hiding details irrelevant to the question at hand, and bringing together related and useful details, abstraction reduces complexity and lets you focus on the idea.
Two halves, and both are active choices: hide what does not matter here, gather what does. Neither is automatic, and the phrase "irrelevant to the question at hand" means the same detail can be essential in one program and noise in another.
1.1 The Same Thing, Two Programs
Take a professional League player. What belongs in a class describing one depends entirely on what the program is for:
| A match-statistics tracker | A team payroll system |
|---|---|
| Handle, role, kills, deaths, assists, vision score | Handle, legal name, salary, contract end date, bonus clauses |
| Salary is irrelevant noise | Vision score is irrelevant noise |
Neither list is more correct. A design that includes both is worse than either, because it hides the main idea of both programs behind details the other one needed. That is abstraction failing.
Laid side by side, the design decision is visible as a pattern of what each program refuses to carry:
3.1.A — represent the design of a program using natural language or diagrams that indicate the classes, and the data and procedural abstractions in each class, including all attributes and behaviors. The deliverable is a design, in words or a diagram — not code.
2Data Abstraction — Naming Without Explaining
Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation. It manages complexity by giving data a name without referencing the specific details of the representation. Data can take the form of a single variable or a collection, such as a class or a set of data.
Concretely: you write visionScore and stop thinking about how a vision score is stored. The name stands for the idea; the representation is somewhere else and, for most purposes, nobody's business.
| The abstract property | The concrete representation |
|---|---|
| "the player's handle" | a String |
| "how many games they have played" | an int |
| "the starting five" | an array of five Player objects |
The two columns are two floors of the same building, and there is a seam between them:
Change the right-hand column and the left-hand column still reads the same. If "the starting five" later becomes an ArrayList instead of an array, every piece of code that only ever asked for the starting five keeps working. Code that reached in and indexed the array directly does not. The separation is what makes the change survivable.
2.1 A Single Variable Counts
The essential knowledge says data can be a single variable or a collection. Naming one number is already data abstraction:
private int gamesPlayed; // the idea has a name; how it is stored is separate
This is why the topic is not only about classes. Every well-named variable is a small act of the same thing.
3Attributes
An attribute is a type of data abstraction defined in a class outside any method or constructor. An instance variable is an attribute whose value is unique to each instance. A class variable is an attribute shared by all instances.
public class Player
{
private static String team = "T1"; // class variable — one copy, shared
private String handle; // instance variable — one per object
private String role;
private int gamesPlayed;
public Player(String handle, String role)
{
int setupCounter = 0; // NOT an attribute — inside a constructor
this.handle = handle;
this.role = role;
}
}
| Declared | Attribute? | Which kind |
|---|---|---|
team | Yes | Class variable — shared by all instances |
handle, role, gamesPlayed | Yes | Instance variables — unique per instance |
setupCounter | No | Declared inside a constructor, so it is a local variable |
The definition is positional, not about types or keywords. A variable declared in the class body is an attribute; the same declaration moved inside a method is not, no matter what it holds. Questions test this by putting a plausible-looking declaration in the wrong place.
Doran, Oner, Faker, Peyz and Keria each get their own handle, role and gamesPlayed — five copies of each. There is exactly one team, and all five share it. Change it and it changes for all of them, because there was only ever one.
4Procedural Abstraction
Procedural abstraction provides a name for a process and allows a method to be used knowing only what it does, not how it does it.
Data abstraction names a thing; procedural abstraction names an action. Both work the same way — a name in front, the details behind it.
player.addGame(7, 2, 11); // what it does: records one game
double kda = player.getKda(); // what it does: returns the KDA ratio
Whether getKda() recomputes from stored totals or returns a cached number is invisible at the call site — and that invisibility is the point, not a side effect.
4.1 Method Decomposition
Through method decomposition, a programmer breaks down larger behaviors of the class into smaller behaviors by creating methods to represent each individual smaller behavior.
One large behavior — "produce a post-game report" — decomposes into several named ones:
public String postGameReport()
{
return headline() + statLine() + formNote();
}
private String headline() { /* ... */ }
private String statLine() { /* ... */ }
private String formNote() { /* ... */ }
postGameReport() now reads as a description of itself. Each smaller behavior can be understood, changed, or fixed without holding the other two in your head — which is the complexity reduction the topic keeps pointing at.
4.2 Extracting Shared Features Instead of Duplicating
A procedural abstraction may extract shared features to generalize functionality instead of duplicating code. This allows for code reuse, which helps manage complexity.
Duplication first — three near-identical blocks:
double doranKda = (doranKills + doranAssists) / (double) doranDeaths;
double onerKda = (onerKills + onerAssists) / (double) onerDeaths;
double fakerKda = (fakerKills + fakerAssists) / (double) fakerDeaths;
Extracted into one generalized behavior:
public static double kda(int kills, int deaths, int assists)
{
return (kills + assists) / (double) deaths;
}
The three-line version contains the formula three times, so a correction has to be made three times — and a fix applied twice out of three is a bug that looks fixed. The extracted version has one formula in one place. Managing complexity is not about shorter code; it is about fewer places where the same decision lives.
5Writing the Design Down
The learning objective asks for a representation in natural language or diagrams, listing the classes and, for each, all attributes and behaviors.
Class Player
Attributes: the team the player belongs to, shared by every player (class variable); and for each individual player, a handle, a role, and the number of games played.
Behaviors: record the result of one game; report the player's KDA; report whether the player has played a minimum number of games; describe the player as a line of text.
- Every attribute earns its place. If no behavior reads or changes it, the program does not need it.
- Every behavior is one thing. "Record the game and print the report and update the standings" is three behaviors wearing one name.
- Nothing names a representation. "an array of five players" is a decision about storage; "the starting five" is the abstraction. Write the second.
6Check Yourself
Is a variable declared inside a constructor an attribute?
No. An attribute is defined in a class outside any method or constructor. A variable declared inside a constructor is local to it — it disappears when the constructor finishes and no object retains it.
What is the difference between an instance variable and a class variable?
Both are attributes. An instance variable's value is unique to each instance — five players, five handles. A class variable is shared by all instances — five players, one team name between them.
A program tracks players for a stats site and stores each player's salary. Good design?
No. Abstraction means hiding details irrelevant to the question at hand, and a stats site never asks about salary. Carrying it adds complexity, invites the data to drift out of date, and obscures the main idea of the class.
How does extracting a repeated calculation into a method manage complexity?
It generalizes the shared feature instead of duplicating code, which allows code reuse. The formula then exists in exactly one place, so it can be understood once and corrected once — and callers use it knowing only what it does, not how.
7Summary
| Idea | What to remember |
|---|---|
| Abstraction | Reduce complexity by focusing on the main idea; hide the irrelevant, gather the useful |
| Data abstraction | Separates abstract properties from concrete representation; names data without naming its storage |
| Attribute | Data abstraction defined in a class outside any method or constructor |
| Instance variable | Attribute unique to each instance |
| Class variable | Attribute shared by all instances |
| Procedural abstraction | Names a process; use a method knowing what it does, not how |
| Method decomposition | Break a larger behavior into smaller named behaviors |
| Extraction | Generalize shared features instead of duplicating — code reuse manages complexity |
Before writing a class, answer two questions in plain sentences: what does this need to know? and what does it need to be able to do? The first list becomes attributes, the second becomes behaviors, and anything that fits neither belongs to a different program.