EMZETT.
Login

Packages/API

In short: A package is a namespace that groups related classes; an API (Application Programming Interface) is the set of publicly usable classes, methods, and functions that a library or system offers externally.

In more detail: Packages prevent naming collisions (two classes can carry the same name if they sit in different packages) and structure larger projects thematically. An API defines the “contract interface” to a system — how to use it without knowing the internal implementation — and is thus closely related to the concept of abstraction.

In Depth

A package is basically a folder with meaning: it groups related classes and turns a class’s complete, unambiguous name into a combination of package path and class name:

package de.emzett.wiki.models;
 
public class Article { ... }
// full name: de.emzett.wiki.models.Article

This way, two completely different libraries can each define a class called User with no conflict, as long as they sit in different packages (com.company1.User vs. com.company2.User) — the full, qualified name is always unambiguous, even if the short class name occurs multiple times.

An API (Application Programming Interface), by contrast, isn’t a technical language feature, but a design concept: the deliberately chosen, publicly accessible “surface” of a system through which other programs/developers are allowed to interact with it — anything that’s NOT part of this API (internal helper classes, private implementation details) counts as an implementation detail that can change at any time, without users of the API being affected by it. This separation is a practical application of encapsulation at the project level rather than the class level.

A well-designed API is characterised above all by stability: once published, public methods should ideally no longer be changed in a way that breaks existing user code — larger APIs often use version numbers for this (e.g. semantic versioning) and mark outdated parts as “deprecated” instead of abruptly removing them, to give users time to adapt.

See also: Classes, Abstraction, Interface