Skip to content
QuizMaker logoQuizMaker
Activity
Java Backend Interview Prep

No lessons available

CONTENTS

11. OOP Relationships: Association, Aggregation, and Composition

Learn how object relationships differ and how to explain them in interviews.

Java Backend Interview Prep
3. OOP & DSA Revision
May 29, 2026
21
A

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.

Share this article

Test your knowledge

Take a quick quiz based on this chapter.

mediumJava Backend Interview Prep
Quiz: OOP Relationships
8 questions8 min

0 comments

Please login to comment.
No comments yet.
Lesson 1 of 5 in 3. OOP & DSA Revision
Next in 3. OOP & DSA Revision
12. SOLID, Coupling, and Cohesion
Back to Java Backend Interview Prep
Back to moduleCategories