Association vs Aggregation vs Composition in Java
Fast recall
Quick revision
Core answer
new in constructor), lifecycle bound — dies with parent. Maps to CASCADE DELETE. Aggregation: part passed in externally, outlives owner — Spring @Autowired is aggregation. Association: objects interact with no ownership (method parameter, local ref). Test: delete the parent — should the child also be deleted? Yes = composition. No = aggregation. No relationship = association.Whether the candidate understands the lifecycle and ownership differences between the three relationship types — not just UML terminology but how it translates to Java code decisions about object creation and destruction
Treating all three as the same thing or confusing aggregation with composition. Giving UML definitions without showing how the distinction appears in actual Java code — specifically who creates the dependent object and what happens when the parent is garbage collected
Show in code how composition means the part is created inside the whole (no independent lifecycle), while aggregation means the part is passed in and survives the whole's destruction. Connect to real Spring patterns — DI is aggregation by default, inner beans are composition
Interview-ready guide
Interview answer
Association, aggregation, and composition describe three types of relationships between objects in object-oriented design — they differ in how strongly one object's lifecycle is tied to another's. There is no special Java syntax for any of them; the distinction lives entirely in who creates the object and who holds the reference.
Composition — owns and creates:
- Composition is the tightest binding: the owner creates the part using
newdirectly in its constructor —Cardoesthis.engine = new Engine(), meaning the car's construction directly causes the engine's creation - The part cannot exist independently — when
Caris garbage-collected with no other references to its engine, the engine is collected too; this maps toCASCADE DELETEin a relational database - A real giveaway: if you can't create the part without creating the owner first, it's composition
Aggregation — uses but doesn't own:
- Aggregation is a looser relationship: the part is constructed elsewhere and passed in —
Department(Listreceives professors that were created independentlyprofessors) - Professors continue to exist after the department is dissolved — they were never owned by the department, just referenced; the department going away doesn't trigger their destruction
- Spring
@Autowiredis always aggregation — the beans are created and managed by the container, not by the class that receives them
Association — interacts without ownership:
- Association is the loosest: two objects interact but neither creates nor manages the other's lifecycle; the relationship exists only for the duration of an interaction
- This shows up as a method parameter or a temporary local reference — a
Teacherwho grades aStudentdoesn't create the student and doesn't hold a permanent field reference
I recommend the lifecycle test when uncertain: if you delete the parent and the child should disappear with it, that is composition. If the child continues to exist independently, that is aggregation.
Association vs Aggregation vs Composition in Java
Direct Answer
Composition: part created inside owner (new in constructor), lifecycle bound — dies with parent. Maps to CASCADE DELETE. Aggregation: part passed in externally, outlives owner — Spring @Autowired is aggregation. Association: objects interact with no ownership (method parameter, local ref). Test: delete the parent — should the child also be deleted? Yes = composition. No = aggregation. No relationship = association.
Common Mistakes & Pitfalls
Treating all three as the same thing or confusing aggregation with composition. Giving UML definitions without showing how the distinction appears in actual Java code — specifically who creates the dependent object and what happens when the parent is garbage collected
Check your understanding
Answer these without looking back at the article.
- How does the composition vs aggregation distinction translate to CASCADE DELETE vs no-cascade in a database schema?
- Is Spring's @Autowired injection an example of composition or aggregation — and why does that distinction matter for testing?
- In terms of garbage collection, what is the lifecycle difference between a composed object and an aggregated one?
- How would you model the relationship between an Order and its OrderLines — composition or aggregation, and why?
- What is the UML notation difference between filled and hollow diamond for these relationships?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.