Unit 1 · 10 min
1.7 Application Program Interface (API) and Libraries
You do not write everything yourself; you use classes other programmers wrote, guided by their documentation.
Summary, objectives and course and exam description (CED) codes
A library is a collection of classes, and an application programming interface (API) specification tells programmers how to use those classes. Classes are grouped into packages, and each class defines a reference type. Attributes are the data of a class, stored in variables; behaviors are what instances can do, defined by methods.
By the end of this session you can
- 1.7.A Identify the attributes and behaviors of a class found in the libraries contained in an API.
Reviewed against the CED on
1LearnThe notes, the traps and how to remember them
1. Libraries, APIs and packages
You do not write every class yourself. A library is a collection of classes. An application programming interface (API) specification informs the programmer how to use those classes. To understand what attributes and behaviors a class in the API has, you read its documentation 1.7.A.1.
- A class defines a specific reference type.
Stringis a class, soStringis a reference type 1.7.A.1. - Classes in APIs and libraries are grouped into packages 1.7.A.1.
- Existing classes and class libraries can be used to create objects 1.7.A.1.
Example: String and Math are classes in the java.lang package, part of the library that comes with Java. The Java Quick Reference tables in topics 1.11 and 1.15 are short extracts of their API documentation.
2. Using a class you did not write
You can use a library class by reading its documentation. You do not need to see its code.
String city = "Lisbon";
System.out.println(city.length());
System.out.println(Math.sqrt(81.0));
Output:
6
9.0
The String documentation says length() returns the number of characters. The Math documentation says sqrt returns a square root. Neither class was written by you.
3. Attributes and behaviors
Attributes are the data that belong to a class; they are kept in variables. Behaviors are the actions an instance of the class can do, or that can be done to it; methods define them 1.7.A.2.
Example: a Plant class might have the attributes name and height and the behaviors grow, getName and getHeight. Its documentation could look like this:
| Documentation entry | Attribute or behavior? |
|---|---|
String name: the plant’s name |
attribute (data in a variable) |
int height: the height in centimeters |
attribute (data in a variable) |
void grow(int cm): adds cm to the height |
behavior (defined by a method) |
int getHeight(): returns the height |
behavior (defined by a method) |
String getName(): returns the name |
behavior (defined by a method) |
Using the behaviors changes or reports the attributes:
Plant fern = new Plant("Fern", 30);
fern.grow(5);
System.out.println(fern.getName() + " is " + fern.getHeight() + " cm");
Output:
Fern is 35 cm
Watch out: attribute = noun stored in a variable (height); behavior = verb defined by a method (grow). In documentation, a method name is followed by parentheses.
Lock it in: memory hooks
- Attribute vs behaviorAttribute = noun stored in a variable; behavior = verb defined by a method.
- Library vs APIThe library is the toolbox; the API is the instruction sheet.
Common misconceptions
"You have to write every class you use."
Not quite. Existing classes and class libraries can be used to create objects. String and Math come with Java, and their API documentation tells you how to use them.
"A method is an attribute because it belongs to the class."
Not quite. Attributes are data stored in variables (nouns, such as height). Behaviors are actions defined by methods (verbs, such as grow).
"An API is a program you run."
Not quite. An API specification is documentation. It informs the programmer how to use the classes in a library.
Exam traps
- Attribute = noun stored in a variable (height); behavior = verb defined by a method (grow).
- A method name is followed by parentheses in documentation, such as grow(int cm). A variable such as height has no parentheses.
2PracticeRecall it, then check yourself
In 30 seconds
- A library is a collection of classes.
- An API specification is documentation that tells programmers how to use those classes.
- A class defines a specific reference type; classes are grouped into packages.
- Existing classes can be used to create objects.
- Attributes: data related to a class, stored in variables. Behaviors: what instances can do, defined by methods.
Cover this list, say the points out loud, then take the checkpoint without scrolling back up.
Checkpoint · 4 questions
Checkpoint: Application Program Interface (API) and Libraries
Pick an answer to mark it. Every answer is explained, and you can try any question again. Your saved score counts first tries only, and stays in this browser.
-
Thermostat documentation Entry Description double targetTemp The temperature to reach boolean heaterOn Whether the heater is running void raise(double amount) Raises the target temperature by amount double getTarget() Returns the target temperature
3PrevailLock it in for exam day
By the numbers
- What the documentation of a class describes two parts CED 1.7.A.2
- attributesdata stored in variables
- behaviorsactions defined by methods
Key terms: flip the cards
Read the term, say the definition out loud, then flip to check. Struggling a little to remember is what makes it stick.
-
Application programming interface (API)tap to flip
An API specification is documentation that tells programmers how to use the classes in a library: their attributes and behaviors.
Example and CED codes -
Librarytap to flip
A collection of classes that programmers can use instead of writing everything themselves.
Example and CED codes -
Packagetap to flip
A group of related classes in an API or library, such as java.lang, which holds String and Math.
Example and CED codes -
Classtap to flip
The formal implementation, or blueprint, of the attributes and behaviors of an object. A class defines a reference type.
Example and CED codes -
Attributetap to flip
Data related to a class, stored in variables, such as a plant's name and height.
Example and CED codes -
Behaviortap to flip
What instances of a class can do, or what can be done with them, defined by methods, such as grow or getHeight.
Example and CED codes -
Reference typetap to flip
A data type used to define objects, that is, anything that is not a primitive type. Every class defines a reference type.
Example and CED codes
Questions students ask
What is an API in Java?
API stands for application programming interface. An API specification is documentation that tells programmers how to use the classes in a library, including their attributes and behaviors.
What is the difference between a library and a package?
A library is a collection of classes. Inside APIs and libraries, related classes are grouped into packages, such as the java.lang package that holds String and Math.
What is the difference between an attribute and a behavior?
An attribute is data related to a class and is stored in a variable, such as a plant's height. A behavior is something an instance can do, or that can be done with it, and is defined by a method, such as grow.