TOPIC 1.7 · STUDY
Application Program Interface (API) and Libraries
A library is a collection of reusable classes written by others. An API is the contract that describes how to use those documented classes, their attributes, and their behaviors. Related classes are grouped into packages. The API does not reveal the hidden implementation or fix your algorithm.
1.7.A
Libraries, APIs, and packages
A library is a collection of reusable classes written by other programmers. Instead of inventing every type from scratch, you call classes that already exist. Those classes still define reference types: each one names attributes (stored data) and behaviors (methods you may call).
An API (application program interface) is the contract for those classes. It lists the documented names, what each attribute means, what each method expects, and what it returns or changes. You write client code against that contract. The API does not show the hidden source inside the library, and it does not rewrite or correct your own algorithm.
public double readMoisture()
That fictional header is the kind of detail an API publishes. From the contract alone you know the method name, that it takes no arguments, and that it returns a double. You do not learn how the sensor is polled, and you do not learn whether calling it at the right moment solves your watering problem.
| Term | What it is | What it is not |
|---|---|---|
| Library | A collection of reusable classes written by others | Your one client program or a single method call |
| API | The contract for documented classes, attributes, and behaviors | Hidden implementation code or a check that your algorithm is right |
| Package | A group of related library classes | A place where undocumented internals become fair game |
The API arrow aims at documented class use inside the package. It does not point into the hidden implementation.
Related classes are grouped into packages so you can find them and keep names organized. garden.tools might hold SoilProbe and WaterTimer together. The package name helps you locate the classes. It still does not open the hidden code, and it still does not approve every call sequence you invent.
Key points
- A library is a collection of reusable classes written by others.
- An API is the contract for documented classes, attributes, and behaviors.
- Use only documented members; do not invent hidden methods as if they were part of the contract.
- Related classes are grouped into packages.
- The API does not reveal the hidden implementation or correct your algorithm.
Reading the API does not prove your own algorithm is correct. The contract tells you how documented members may be used. Choosing the right call in the right order, and checking the result against the problem, is still your responsibility.