TOPIC 3.1 · STUDY

Abstraction and Program Design

A class names one kind of object. Attributes store that object's data; behaviors are the actions it can perform. Data abstraction hides how values are stored; procedural abstraction hides the steps inside a method.

3.1.A

Classes, attributes, and behaviors

Program design starts by naming the kinds of objects a program will use. A class is that kind: the shared idea of one type of thing. Later topics show how to write the Java class body. Here you only identify what belongs in the design.

An attribute is stored data about one object or about the whole class. Examples include a bin number or a color. A behavior is an action the object can carry out, such as mark claimed or cancel a hold. Keep those roles straight: numbers and labels are data; verbs that change or report something are behaviors.

One CoatCheckTag class box lists two stored attributes, rackSlot and colorCode, above one behavior, markClaimed.

The class names the kind of object. rackSlot and colorCode are attributes. markClaimed is a behavior.

Piece Role Everyday meaning
CoatCheckTag Class The kind of object being designed.
rackSlot Attribute Stored data for one tag's rack place.
colorCode Attribute Stored data for one tag's color.
markClaimed Behavior An action that tag can carry out.

Some attributes belong to one object; others are shared by every object of the class. Instance attributes hold a value for a single object, such as each coat tag's own rack slot. Class attributes hold one shared value for the whole type, such as one desk fee that every tag uses. Put unrelated data from different kinds of things into separate classes, not one mixed helper.

Data abstraction means callers need not know how the data is stored. They use methods instead of peeking at the private representation. Procedural abstraction means callers need not know the steps inside a method. If the method header and the specified result stay the same, programmers may change hidden storage or hidden steps.

public void heatFor(int minutes)

A short header like that is enough for callers. They pass how many minutes to heat and expect that timing. They do not need the motor formula inside. A parameter generalizes one procedure so the same method works for several durations instead of copying nearly identical methods. Large behaviors can also be split into helper methods that the main method calls in order; that decomposition is still procedural abstraction.

Key points

  • A class is the kind of object; it is not one stored value or one action.
  • Attributes store data; behaviors are actions the object can perform.
  • Instance attributes are per object; class attributes are shared by the type.
  • Data abstraction hides how values are stored from callers.
  • Procedural abstraction hides method steps; parameters and helpers keep designs flexible.

An action is a behavior. Stored data is an attribute. Naming the class correctly does not turn a color or a bin number into a behavior, and a verb such as mark claimed does not become stored data.

Practice this topic