← Back to quizzesFree quiz

Object-Oriented Relationships and Association Classes

Object‑oriented design relies heavily on the way classes relate to one another. In Unified Modeling Language (UML) three primary relationships are used to express ownership, life‑cycle, and…

10 questions~5 min
Object-Oriented Relationships and Association Classes — Qwi
0 / 10
Score: 0%
1

In UML, which notation distinguishes composition from aggregation?

2

When should aggregation be preferred over composition in a class design?

3

Given the Java classes Car and Engine where Car has a final Engine field, which relationship does this illustrate?

4

In the Organization‑Person example, why is aggregation the appropriate modeling choice?

5

What is the main advantage of using an association class to model employment periods between Person and Company?

6

If a Person can have multiple Employment instances with the same Company over time, what modeling change is required?

7

Which of the following statements about self‑association is correct?

8

In the provided StereoSystem‑Car example, which UML relationship best describes the link between Car and StereoSystem?

9

Why does composition improve encapsulation compared to exposing inner objects directly?

10

When modeling a job that includes salary and boss information for a Person, why is a Job association class preferable to storing these attributes in Person?

Understanding Object‑Oriented Relationships in UML

Object‑oriented design relies heavily on the way classes relate to one another. In Unified Modeling Language (UML) three primary relationships are used to express ownership, life‑cycle, and collaboration: composition, aggregation, and association. This course explains each relationship, shows how to recognize them in code, and demonstrates advanced modeling techniques such as association classes and self‑association. By the end of the lesson you will be able to choose the correct relationship for any domain problem and model it with clear, SEO‑friendly UML diagrams.

Composition vs. Aggregation: Visual Notation

Both composition and aggregation are forms of whole‑part relationships, but they differ in ownership and life‑cycle semantics.

  • Composition: Represented by a filled diamond on the composite (whole) end of a solid line. The part cannot exist independently of the whole. When the whole is destroyed, all its parts are destroyed as well.
  • Aggregation: Represented by an unfilled diamond on the whole end of a solid line. The part may exist independently; the whole merely references the part.

In a quiz, the correct answer to “Which notation distinguishes composition from aggregation?” is “A filled diamond on the composite end.” This visual cue instantly tells a reader whether the relationship is strong (composition) or weak (aggregation).

When to Prefer Aggregation Over Composition

Choosing aggregation is appropriate when the part objects have an independent life‑cycle. Consider an Organization that employs many Person objects. A person can belong to zero, one, or many organizations over time, and they continue to exist even if the organization is deleted. Therefore, the relationship is an aggregation rather than a composition.

Key guideline:

  • If the part can be shared, moved, or reused outside the whole, use aggregation.
  • If the whole strictly controls the creation and destruction of the part, use composition.

Real‑World Code Example: Car and Engine

In Java, a Car class that holds a final Engine field illustrates composition:

public class Car {
    private final Engine engine = new Engine();
  }

The Engine instance is created inside the Car constructor and cannot exist without the Car. Deleting the Car implicitly destroys its Engine, matching the definition of composition. The quiz confirms this by selecting the answer “Composition, because the Engine cannot exist without the Car.”

Modeling Many‑to‑Many Relationships with Association Classes

Sometimes a relationship itself carries data. The classic example is employment periods between Person and Company. A person may work for many companies, and a company may employ many people. The link between them needs extra attributes such as startDate, endDate, and position. An association class solves this problem.

  • The association class (e.g., Employment) is drawn as a class attached to the association line.
  • It stores attributes that belong to the relationship, not to either endpoint.

The quiz question “What is the main advantage of using an association class?” is answered correctly with “It allows storing attributes that belong to the relationship itself.” This highlights why association classes are essential for many‑to‑many links that need their own state.

Extending the Model: Multiple Employment Instances

If a Person can have several Employment records with the same Company over time, the simple association class must be promoted to a full-fledged class. This change enables each employment period to be a distinct object with its own identity and attributes.

The correct quiz answer is “Promote Employment to a full class separate from the association.” By treating Employment as an independent class, you can model a one‑to‑many relationship from Person to Employment and from Company to Employment, preserving the history of each contract.

Self‑Association Explained

A self‑association occurs when a class relates to another instance of the same class. A classic scenario is an Employee who may have a manager who is also an Employee. The relationship is drawn as a line that loops back to the same class.

  • It does not require static fields; each instance holds its own reference.
  • It is distinct from inheritance; self‑association models a peer‑to‑peer link, not an “is‑a” relationship.
  • Only one instance is linked to another; the two objects can be different instances.

The quiz confirms the correct statement: “An instance can be linked to another instance of the same class.” This principle is crucial when designing hierarchical structures such as trees or graphs.

Case Study: Car and StereoSystem

Consider a Car that may optionally contain a StereoSystem. The stereo can be installed or removed without affecting the car’s existence, and the same stereo model could be used in multiple cars. This scenario matches aggregation:

  • Car aggregates StereoSystem – the car can exist with or without the stereo.
  • The relationship is drawn with an unfilled diamond on the Car side.

The quiz answer “Aggregation, because Car can be created with or without a StereoSystem” reflects this reasoning.

Best Practices for Choosing the Right Relationship

When modeling a system, follow this decision flow:

  1. Is the part’s life‑cycle bound to the whole?
    • Yes → Use composition (filled diamond).
    • No → Continue to step 2.
  2. Can the part be shared among multiple wholes?
    • Yes → Use aggregation (unfilled diamond).
    • No → Consider a simple association (plain line).
  3. Does the relationship itself need attributes?
    • Yes → Introduce an association class.
    • No → Keep the plain association.
  4. Is the relationship between instances of the same class?
    • Yes → Model a self‑association.

Applying this checklist ensures that your UML diagrams convey the intended semantics and that your code respects proper ownership rules.

SEO‑Friendly Summary

Keywords such as UML composition vs aggregation, association class example, self‑association in UML, and object‑oriented relationship modeling are strategically placed throughout this guide. Search engines favor clear headings (<h2>, <h3>), concise paragraphs, and bullet lists—all of which are employed here to improve discoverability for developers seeking guidance on class relationships.

Key Takeaways

  • Composition uses a filled diamond and implies exclusive ownership and shared life‑cycle.
  • Aggregation uses an unfilled diamond and indicates a weaker whole‑part link where parts can exist independently.
  • Association classes capture attributes that belong to the relationship itself, essential for many‑to‑many links like employment periods.
  • Self‑association models links between instances of the same class, useful for hierarchical structures.
  • Follow a systematic decision process to select the appropriate UML relationship for any design problem.