Skip to main content

Unit 3 · Topic 3.1

3.1 Abstraction and Program Design

Abstraction is how programmers manage complexity: you hide the details that don't matter right now so you can think about the ones that do. This topic covers data abstraction, procedural abstraction and planning a class's attributes and behaviors before you write any code.

Key terms

  • abstraction
  • data abstraction
  • procedural abstraction
  • method decomposition
  • instance variable
  • class variable

What abstraction means

Abstraction makes a problem simpler to think about: you keep the big idea in view and leave out the details that don't matter for the job in front of you. A map is an abstraction of a city: it keeps the streets and drops the color of every house. When you drive a car, you use the steering wheel and pedals without knowing how the engine works.

Data abstraction

Data abstraction gives data a name without showing how it's stored. When you use a String, you think of it as text, not as the memory layout behind it.

In a class, each attribute is a piece of data defined outside any method or constructor. There are two kinds:

  • An instance variable has its own value in every object. Each Book object has its own title.
  • A class variable is shared by all objects of the class. A count of how many Book objects exist would be one shared value. You'll write these with static in 3.7.

Procedural abstraction

Procedural abstraction gives a name to a process, so you can use a method knowing only what it does, not how it does it. You've done this all year with Math.sqrt and substring.

Method decomposition means breaking a large behavior into smaller methods, each doing one job. If the same steps appear in two places, move them into one method and call it twice. Parameters make a method general, so one method can handle a whole range of inputs instead of one fixed case. Less repeated code means fewer places for bugs to hide.

Abstraction also lets you change how a method works inside, to make it faster or use less memory, without telling anyone who calls it, as long as its header and what it does stay the same.

Designing a class first

Before you write a class, plan it. List its attributes (the data each object has) and its behaviors (the methods). You can write the plan in plain sentences or draw a class diagram: a box with the class name at the top, the attributes in the middle and the methods at the bottom. This plan becomes the skeleton of your code, and it's exactly how free-response Question 2 describes the class you have to write.

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    Planning a class from a description

    A school library app needs a class to represent a book. Each book has a title and a number of copies the library owns, and the app tracks how many of those copies are checked out right now. Users can check a copy out, return a copy, and ask whether any copy is available. The app also keeps one running total of all checkouts across every book. List the attributes (saying which are instance variables and which is a class variable) and the behaviors.

    Show the solution
    1. Step 1: Pick out the nouns that describe each book's data: title, copies owned, copies checked out. Each book has its own values, so these are instance variables: a String and two ints.
    2. Step 2: "One running total" across every book is shared, so it's a class variable, an int.
    3. Step 3: Pick out the verbs for behaviors: check out a copy, return a copy, and check availability. Availability is a yes-or-no question, so that method returns a boolean.
    4. Step 4: You'd also want a constructor that takes the title and the number of copies, with checked-out copies starting at 0.

    Answer: Instance variables: title (String), copies owned (int), copies checked out (int). Class variable: total checkouts (int). Behaviors: a constructor, check out, return, and is-available (returns boolean).

  2. Example 2

    Removing repeated code

    This code repeats the same steps with a different stopping number. Rewrite it with a method so the steps appear only once.int total = 0; for (int i = 1; i <= 4; i++) { total += i * i; } System.out.println(total); int total2 = 0; for (int i = 1; i <= 7; i++) { total2 += i * i; } System.out.println(total2);

    Show the solution
    1. Step 1: Both loops add up the squares from 1 to some number. The only difference is that number, 4 or 7, so it becomes a parameter, n.
    2. Step 2: The method returns the total instead of printing it, so a caller can use the result however it likes.
    3. Step 3: public static int sumOfSquares(int n) { int total = 0; for (int i = 1; i <= n; i++) { total += i * i; } return total; }
    4. Step 4: Now the calling code is just:
    5. Step 5: System.out.println(sumOfSquares(4)); System.out.println(sumOfSquares(7));
    6. Step 6: Later you could swap the loop for the formula n(n + 1)(2n + 1) / 6 and nobody calling sumOfSquares would need to change anything. That's procedural abstraction.

    Answer: Write sumOfSquares(int n) as shown and call it twice. Both versions print 30 and then 140.

Common mistakes

  • Making a value an instance variable when it should be shared by every object, or the other way around. Ask: does each object need its own value?
  • Copying and pasting the same steps instead of writing one method with a parameter.
  • Confusing attributes (data, nouns) with behaviors (methods, verbs) when planning a class.

On the exam

  • Expect multiple-choice questions about which attributes and methods a class design needs, or which change would reduce repeated code.
  • On free-response Question 2, read the description and the table of example calls, and list the instance variables you'll need before you start typing.

Connected topics

Videos

Check yourself

4 questions on 3.1 Abstraction and Program Design. Pick an answer to see if you got it, and why.

Question 1 of 4

A student calls Math.sqrt(50.0) in her program without knowing how the method computes a square root. She only knows what it returns. This is an example of which of the following?

Question 2 of 4

A GradeReport class has one very long method that computes a student's average, finds the highest score, counts missing assignments and prints everything. The same averaging code also appears in two other methods. Which change best improves the design?

Question 3 of 4

A programmer rewrites the body of a method findTotal so that it runs faster. The method's header and what it returns stay exactly the same. Which of the following is true?

Question 4 of 4

A Student class is being designed. Which of the following attributes is best stored in a class variable, shared by all Student objects, rather than in an instance variable?

0 of 4 answered