Association, aggregation and composition describe how one class relates to another by holding a reference to it, and they differ in how strongly the objects depend on each other. Association is any "uses" link between two classes, aggregation is a "has-a" link where the part can exist without the whole, and composition is a "has-a" link where the part cannot outlive the whole. These are design terms from UML, not Java keywords, so you express them through fields, constructors and object lifetimes.
Understanding the difference helps you decide who creates an object, who owns it and what should happen when the owner goes away.
| Relationship | Meaning | Lifetime of the part | Example |
|---|---|---|---|
| Association | One object uses or knows another | Independent | Teacher and Student |
| Aggregation | Whole has parts, weak ownership | Part survives the whole | Department and Professor |
| Composition | Whole has parts, strong ownership | Part dies with the whole | House and Room |
Aggregation and composition are both special kinds of association. Think of them as a scale from "knows about" to "owns completely".
Association means that objects of one class hold or receive a reference to objects of another class, with no ownership implied. It can be one-to-one, one-to-many or many-to-many.
A teacher knows many students and a student can be taught by many teachers. Neither owns the other, and either can exist alone.
In aggregation the whole holds parts, but the parts are created elsewhere and passed in. If the whole is discarded, the parts still exist and may belong to other wholes.
The tell-tale signs of aggregation are that the part is passed into a constructor or method and that the whole does not create or destroy it.
In composition the whole creates its parts and controls their lifetime. Nobody else should hold a reference to a part, so when the whole becomes unreachable, so do its parts.
A Room makes no sense without a House, so it is created inside the house, hidden from the outside and never passed in. A private nested class is a convenient way to enforce this in Java.
Java has no syntax that separates the three, so look at the code for these clues:
Be pragmatic. The boundary between aggregation and composition is a modelling decision, and the same classes could be modelled either way depending on your domain. Many teams use only "association" and "composition" and treat aggregation as an association with a collection.
All three relationships are forms of "has-a", in contrast to inheritance, which is "is-a". They are the tools behind the advice to prefer composition over inheritance. Persistence frameworks also reflect them: for example, JPA lets you declare cascade and orphanRemoval on a relationship so that deleting an order also deletes its lines, which is composition-style ownership.
In aggregation the parts can exist without the whole and are usually passed in from outside. In composition the whole creates and owns the parts, and they should not outlive it.
Aggregation is a stronger, more specific association that says "the whole has parts". Plain association only says two classes are connected.
No. They are modelling concepts expressed with fields, constructors and lifetimes. UML diagrams draw them with an open diamond for aggregation and a filled diamond for composition.
Use composition when the part has no meaning without the whole, aggregation when parts are shared or long-lived on their own, and association for loose collaboration.
Association is a general link, aggregation is a has-a link with independent parts, and composition is a has-a link in which the whole owns and controls the parts. In Java you tell them apart by who creates the part, who else references it and what should happen when the whole is removed.