Association
Association means one object knows about or uses another object. It can be one-to-one, one-to-many, many-to-one, or many-to-many.
Example: a Bank has many Employees.
Aggregation
Aggregation is a weak "has-a" relationship. The child can exist independently of the parent.
Example: a Department has Employees. If the Department is removed, the Employee can still exist.
Composition
Composition is a strong "part-of" relationship. The child lifecycle depends on the parent.
Example: a House has Rooms. If the House is deleted, the rooms as part of that house do not meaningfully exist.
UML Hint
Aggregation is shown with an empty diamond. Composition is shown with a filled diamond.
Interview Scenario Practice
Scenario 1: Bank and Employee
Scenario: A bank has employees, but employees can still exist if the bank record is removed.
Strong answer: This is aggregation. It is a weak has-a relationship where the child can have an independent lifecycle.
Why it works: The relationship exists, but ownership is not lifecycle-dependent.
Common mistake: Calling every has-a relationship composition.
Scenario 2: House and Room
Scenario: Rooms are modeled only as parts of a specific house.
Strong answer: This is composition. The child object is part of the parent and depends on the parent lifecycle.
Why it works: Composition expresses strong ownership.
Common mistake: Saying composition and aggregation are the same because both are has-a relationships.
Scenario 3: Service Uses Repository
Scenario: An order service calls an order repository to save data.
Strong answer: This is association/dependency, not necessarily ownership. The service uses the repository to perform behavior.
Why it works: Association is the broad relationship where one object communicates with another.
Common mistake: Over-modeling simple dependencies as composition.