Lesson 3.1: Abstraction and Program Design
Lesson 3.1: Abstraction and Program Design
What You'll Learn
- Define abstraction and explain why it is central to class design
- Distinguish instance variables from class (static) variables
- Apply the three-question method to design a class from a specification
- Explain why instance variables should be private
Key Vocabulary
| Term | Definition |
|---|---|
| abstraction | Reducing complexity by focusing on the main idea; hiding irrelevant details (EK 3.1.A.1) |
| data abstraction | Separates what data represents from how it is stored; gives data a name without exposing implementation (EK 3.1.A.2) |
| attribute | Data defined in a class outside any method; represents a property of objects (EK 3.1.A.3) |
| instance variable | An attribute whose value is unique to each instance of the class (EK 3.1.A.3) |
| class variable | An attribute shared by ALL instances; declared with the static keyword (EK 3.1.A.3) |
| behavior | An action a class can perform; implemented as a method |
| UML diagram | Unified Modeling Language diagram showing a class's attributes and behaviors |
CED EK 3.1.A.1–3.1.A.3
Abstraction reduces complexity by hiding implementation details and exposing only what is necessary. An instance variable's value is unique per object; a class variable (static) is shared by all objects.
What Is Abstraction?
A BankAccount class abstracts a real account. You include balance and methods like deposit(). You ignore the bank's server architecture and network protocols.
Identifying Attributes and Behaviors
Scenario: model a Student in a grade-book program.
| Category | Examples |
|---|---|
| Attributes (data) |
name (String), grade (int), gpa (double) |
| Behaviors (methods) |
getGpa(), setGrade(int g), toString()
|
| Irrelevant details | home address, locker number, lunch preference |
Instance Variables vs Class Variables
Instance vs Class Variable
public class Student {
private String name; // instance variable -- unique per object
private int grade;
private static int total = 0; // class variable -- shared by ALL objects
public Student(String n, int g) {
name = n; grade = g;
total++; // increments the ONE shared copy
}
}
After creating two Students, each has its own name, but total is 2 for both.
Designing a Class: Three Questions
| Question | Guides You To |
|---|---|
| What is it? | The class name (a noun) |
| What does it have? | Instance variables (attributes) |
| What can it do? | Methods (behaviors) |
AP Trap: Attributes vs Behaviors
Attributes are nouns (data stored in the object). Behaviors are verbs (actions the object performs). getBalance() is a behavior, NOT an attribute.
AP Trap: Static vs Instance
Every object needs its OWN copy → instance variable. All objects share ONE copy → class variable (static). A counter of objects created is almost always static.
AP Trap: Private Instance Variables
Instance variables should be private unless the specification says otherwise. Public instance variables break encapsulation and appear as distractors on the AP exam.
Real-World Connection: A GPS app models roads as objects with attributes (speedLimit, laneCount) and behaviors (getCongestion()).
Summary
- Abstraction hides irrelevant details and focuses on essential properties.
- An instance variable belongs to a specific object; a class variable (static) is shared by all objects.
- Design a class by identifying its name, attributes, and behaviors.
- Instance variables representing attributes should be declared
private.
Practice Questions
TrafficLight class, which is BEST classified as an attribute (instance variable)?changeToRed()
currentColor
getCycleTime()
flashWarning()
private static int totalCreated; is inside Counter. Which is TRUE?Book class?public class Thermometer {
public double temperature;
public double humidity;
}
static variable?Vehicle class are TRUE?I.
make and model are appropriate instance variables.II.
accelerate() and brake() are appropriate behaviors.III.
roadSurfaceComposition is an appropriate Vehicle instance variable.Dog d1 = new Dog("Rex",3); and Dog d2 = new Dog("Spot",5);, where dogCount is static and incremented per constructor call, which describes the state?Mastery: Abstraction and Program Design
public class Inventory {
public String itemName;
public int quantity;
}
lib1.addCheckout(); lib1.addCheckout(); lib2.addCheckout(); where addCheckout() increments the static variable totalCheckouts, what is the value of totalCheckouts?Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]