Lesson 1.7: Application Program Interface (API) and Libraries | AP CSA
Lesson 1.7: Application Program Interface (API) and Libraries
What you'll learn in this lesson
- 1.7.A. Explain what a library is, what an API specification does, and how classes are organized into packages.
- 1.7.A. Distinguish between a class’s attributes (data stored in variables) and its behaviors (actions defined by methods).
This lesson is part of the AP CSA Course and Exam Description (effective May 2027).
AP exam weight: API vocabulary appears in context: the exam gives you a class diagram or partial documentation and asks you to identify attributes, behaviors, or how to use a class from its specification. You will not be asked to recall specific API details from memory.
Key Vocabulary
| Term | Definition |
|---|---|
| library | A collection of pre-written classes that programmers can use without implementing the underlying code. |
| API | Application Program Interface; documentation specifying how to use a library's classes and their methods. |
| package | A named grouping of related classes in Java, e.g. java.util contains Scanner. |
| attribute | Data stored in a class, defined by a variable (field). Describes what an object has. |
| behavior | An action a class instance can perform, defined by a method. Describes what an object does. |
| procedural abstraction | Using a method by knowing what it does from the API, without knowing how it is internally implemented. |
Libraries: organized collections of classes
A library is a collection of pre-written classes that programmers can use without writing the code themselves. Java’s standard library includes hundreds of classes covering math, input/output, data structures, networking (see the Java SE 17 API documentation), and more.
How Libraries Are Organized
Classes in Java libraries are grouped into packages. A package is a named namespace that keeps related classes together and avoids naming conflicts. For example, Scanner lives in java.util, and Math lives in java.lang. Classes in java.lang are available by default; classes in other packages typically require an import statement.
API: the specification for how to use a library
An Application Program Interface (API) specification documents how to use the classes in a library. It tells you what a class does, what methods it provides, what parameters each method requires, and what each method returns. You do not need to know how a method is implemented internally to use it correctly; you only need to read its API documentation.
Why API Documentation Matters
A class defines a specific reference type. The API specification tells the programmer how to create instances of that type and what they can do with those instances. AP CSA provides a Java Quick Reference during the exam precisely because programmers use API documentation rather than memorizing implementation details.
Attributes and behaviors
Every class has two kinds of members:
The Distinction
Attributes refer to the data related to the class. They are stored in variables (also called fields or instance variables). They describe what an object has.
Behaviors refer to what instances of the class can do. They are defined by methods. They describe what an object does.
Example: a BankAccount class
Attributes: balance (double), accountNumber (String), ownerName (String)
Behaviors: deposit(amount), withdraw(amount), getBalance()
From the API, a programmer knows what deposit does (adds money to the balance) and what parameter it takes (an amount). The programmer does not need to know whether the method uses addition or some other mechanism internally.
AP Trap: attributes vs behaviors
On the exam, a question may show you a class description and ask whether something is an attribute or a behavior. Variables are always attributes; methods are always behaviors. If a question describes something a class “stores” or “tracks,” that is an attribute. If it describes something a class “can do” or “returns,” that is a behavior.
Practice Questions
Student class has the following members: a String variable name, an int variable gradeLevel, and a method getGPA() that returns a double. Which of the following correctly classifies these members?name and gradeLevel are behaviors; getGPA() is an attribute.name and gradeLevel are attributes; getGPA() is a behavior.name and gradeLevel are variables, so they are attributes. Behaviors are defined by methods. getGPA() is a method, so it is a behavior.
ArrayList class from Java’s standard library. According to the API specification, ArrayList has an add(element) method. Which of the following does the API specification allow the programmer to know without reading the implementation code?add method executes.add requires and what effect calling it has on the list.import statement is needed.List without conflict as long as they are in different packages. B is wrong: packages are organizational, not a bytecode cache. C is wrong: access is controlled by access modifiers, not packages. D is wrong: only java.lang classes are available by default; others require import.
Car class has the following members:I. A
String variable modelII. A method
accelerate(int speed)III. A
double variable fuelLevelWhich of the above are attributes?
model (I) and fuelLevel (III) are variables—both are attributes. accelerate (II) is a method, so it is a behavior, not an attribute.
Dog, you create a new type. A variable declared as Dog d holds a reference (memory address) to a Dog object, not the object’s data directly. This is how all reference types work in Java, in contrast to primitive types like int which store values directly.
Math.sqrt(x) from Java’s Math class without knowing how the square root is computed internally. This is an example of which programming concept?Math.sqrt from the API without seeing the source code is a direct example. B, C, and D describe unrelated concepts.
The Library Card System
A developer is designing a LibraryCard class. The class needs to track the cardholder’s name, the number of books currently checked out, and the card’s expiration year. It should also be able to check out a book, return a book, and report whether the card is expired.
Part A: Classify the members
LibraryCard class?checkOut() Behavior: cardholderName
isExpired() Behavior: booksCheckedOut
returnBook() Behavior: expirationYear
booksCheckedOut Behavior: isExpired()
booksCheckedOut is a variable that stores data, making it an attribute. isExpired() is a method that performs an action and returns a result, making it a behavior. In A, B, and C the labels are reversed.
Part B: API usage
LibraryCard but has not seen its source code. The API documentation states: void checkOut(String title) — Adds one book to the checked-out count. Which of the following can the programmer conclude from this documentation alone?LibraryCard object.void checkOut(String title) tells us: return type is void (no return value), the parameter is a String named title, and the description says it increments the count. A is an implementation detail not in the API. B and D are restrictions the API does not mention.
Part C: Structured response
A teammate argues that since all the code in LibraryCard was written by someone else, using it without reading the implementation is risky. In three to five sentences, explain why API documentation makes it safe and efficient to use a class without reading its implementation, and what the key information in an API specification provides to a programmer.
Scoring Rubric (4 points)
- +1: Names the concept: procedural abstraction (or abstraction). The programmer uses the method by knowing what it does, not how.
- +1: Identifies what API documentation provides: method names, parameter types and names, return types, and descriptions of behavior.
- +1: Explains why this is sufficient: if the documentation accurately describes the behavior, the programmer can write correct code that calls the method without access to the internal algorithm.
- +1: Notes a real benefit: the implementer can change the internal algorithm to improve performance without breaking code that uses the class, as long as the documented behavior stays the same.
Javadoc: generating API documentation from code
Java’s javadoc tool reads /** */ comment blocks in source code and generates HTML API documentation automatically. The Java standard library documentation at docs.oracle.com is produced this way. Every public method, parameter, and return value you see in the official docs came from a /** */ comment in the Java source code. Lesson 1.8 covers the comment syntax.
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]