AP Computer Science A · Unit 3 lecture · Part 1 of 10 · 8 min

3.1 Abstraction and program designDesign before you type

By the end: Design and write a complete Java class: choose its private data, write constructors that set it up, write methods that return or change it, and trace exactly what happens with object references, static variables, scope and this.
Mr. Thompkins
Hi, I'm Mr. T. For two units you've used classes other people wrote: String, Math, and the classes the exam hands you. Now you write your own. Here's our first job: a bike-share company wants software for its docks. Before any code, what does a dock need to know, and what does it need to do?

So far you've been a client: a programmer who uses a class by calling its methods. In this unit you also become the author, the programmer who writes the class. Keep both roles in your head, because every design choice is about making life easy and safe for the client.

?Wait, what's the difference between a class and an object again?
A class is a blueprint: it describes what every object of that type knows and can do. An object is one actual thing built from the blueprint with new. One BikeDock class can produce a hundred dock objects, and each one keeps its own data.

Program design starts with two short lists. The attributes are the data each object has to remember between method calls. They become instance variables. The behaviors are the things an object can do. They become methods. For a dock, the attributes are the station name, how many bikes it can hold (its capacity) and how many bikes are in it now. The behaviors are renting a bike out, taking a bike back, and reporting how many bikes are there.

BikeDock- station : String- capacity : int- bikes : int+ BikeDock(String, int)+ rent() : boolean+ returnBike() : boolean+ getBikes() : intattributeswhat it knowsbehaviorswhat it does- means private+ means publicone class: as many docks as you need
A class diagram for BikeDock. Top: the name. Middle: attributes with their types. Bottom: the constructor and methods, with parameter types and return types.

Read a class diagram from top to bottom. The minus sign marks something private (only the class itself can touch it) and the plus sign marks something public (any class can use it). rent() : boolean means a method named rent with no parameters that returns a boolean. The AP exam may describe a class in a diagram like this, in a paragraph of words, or in a table of example calls. All three tell you the same two lists.

Abstraction: hiding what you do not need

Abstraction means ignoring details you don't need right now so you can think about the part you do. It comes in two flavors in this course. Data abstraction: you treat a BikeDock as one thing instead of three loose variables floating around your program. Procedural abstraction: you call dock.rent() knowing what it does, not how it does it. You've been doing this since Unit 1. You call s.length() without ever seeing the code inside String.

?Wait, what's a method signature again?
A method's signature is its name plus its parameter list, like rent() or substring(int, int). The full header also shows the access (public or private) and the return type: public boolean rent(). The signature tells a client how to call it; the return type tells the client what comes back.
1BikeDock dock = new BikeDock("Elm St", 2);12System.out.println(dock.rent());23System.out.println(dock.rent());24System.out.println(dock.rent());35System.out.println(dock.returnBike());46System.out.println(dock.getBikes());4
  1. line 1 Build one dock: station "Elm St", room for 2 bikes, starts full
  2. line 2-3 Two rentals work: true, true
  3. line 4 The dock is empty now, so rent reports false
  4. line 5-6 One bike comes back, so 1 bike is docked
output › true true false true 1
Client code for BikeDock. Every line uses the dock through its methods. Nothing here needs to know how the dock stores its bikes.
Pause and think
Look at the client code above. Which line needs to know the names of the dock's instance variables?
I thought about it. Show me
None of them. The client only knows the constructor and three method headers. That's the whole point of abstraction: the author could rename bikes, or store the count a totally different way, and this client code would still compile and print the same five lines.

Why does this matter so much? Three reasons. First, you can change the inside of a class later, to fix a bug or make it faster, without breaking any client code, as long as the method headers and what they do stay the same. Second, other programmers can use your class after reading only its headers and comments. Third, small pieces with clear jobs are much easier to test than one giant main method.

Design a class
A library wants a class for one study room. Each room has a name and a number of seats, and it keeps track of how many people are inside. A person can enter only if a seat is free, a person can leave, and anyone can ask whether the room is full. List the instance variables with their types, then write the headers of the constructor and methods.
  1. Attributes first: what must each room remember from one call to the next? Its name (String), its number of seats (int) and how many people are inside now (int).
    Why?
    Anything a method needs later has to be stored in an instance variable. A value used only inside one method can be a local variable instead.
  2. Make all three private: private String name; private int seats; private int people;
    Why?
    Private data is the rule in this course. The Class Design FRQ checks for it.
  3. Constructor: public StudyRoom(String name, int seats). Every room starts empty, so people is set to 0 inside the constructor and needs no parameter.
    Why?
    A constructor needs a parameter only for a value that can differ from one new object to the next.
  4. Behaviors: public boolean enter() reports whether the person got in, public void leave() just changes the count, and public boolean isFull() answers a yes or no question.
    Why?
    Pick the return type from the question the method answers. "Did it work?" and "Is it full?" are booleans. "Do this" with nothing to report is void.

Answer: private String name; private int seats; private int people; public StudyRoom(String name, int seats); public boolean enter(); public void leave(); public boolean isFull().

Stuck? Get a hint
  1. Read the description twice: once for nouns (what it knows), once for verbs (what it does).
  2. The nouns are name, seats and the number of people inside. Each becomes a private instance variable.
  3. For each verb, ask what the caller gets back: a yes or no answer means boolean, nothing at all means void.
Check yourself
Which of these is the best example of procedural abstraction?
  1. ACalling dock.rent() without knowing which instance variables it changes
  2. BMaking every instance variable public so clients can see them
  3. CCopying the same five lines into three different methods
  4. DNaming every int variable x so the code is shorter
Show the answer

A. Procedural abstraction means using a method by knowing what it does, not how. The second choice is the top trap: making data public is the opposite of hiding details, and it breaks encapsulation. Copying lines is what a well-named method is supposed to replace.

  • Attributes (what an object knows) become private instance variables. Behaviors (what it does) become methods.
  • A class diagram lists the name, the attributes with types, and the methods with parameter and return types. Minus means private, plus means public.
  • Data abstraction: treat related data as one object. Procedural abstraction: use a method knowing what it does, not how.
  • If the headers and behavior stay the same, the author can rewrite a class's insides without breaking clients.