CS111
Java Contracts

What is a contract/API?

Standard Java Classes
from the Java 6 API
Java Classes Specific to CS111

What is a contract/API?

Every class is characterized by a contract that specifies the behavior of its methods -- that is, how instances of the class respond to messages. For each method, the contract specifies:

(There are additional aspects of the contract that we are intentionally ignoring for the time being.)

The contract defines an abstraction barrier between the users and the implementers of the class. A user of the class can expect that objects will behave as described in the contract, but cannot expect anything more than what is specified in the contract. An implementer of the class must ensure that objects fulfill the contract, but need not implement anything more than what is specified in the contract.

In the programming community, another term for a contract is an Application Programming Interface (API). An API is the documentation that allows programmers to use the components of a software library as black-box abstractions. It turns out that every class in the standard Java distribution is a library component that is described by an API. The API for the entire Java Development Kit (JDK) that we use in class can be found here.

The Java API can be daunting for the uninitiated. Keep in mind that you don't have to understand everything on the first pass. Indeed, there are some things you probably never need to understand! Treat the Java API as a useful reference document that you consult on a need-to-know basis.