100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

UML Class Diagram Relationships Explained

UML class diagram relationships explained — association, aggregation, composition, generalization, and realization — with Java examples.

mediumQ82 of 226 in Object Oriented Programming Est. time: 6 minsLast updated:
Open Code Lab

Expected Interview Answer

A UML class diagram models a system’s classes as boxes with attributes and operations, and connects them with relationship lines — association, aggregation, composition, inheritance (generalization), realization, and dependency — each carrying a distinct semantic meaning and notation.

Association is a plain line meaning two classes interact or hold references to each other; it can be one-way or two-way and carries multiplicity like 1..* at each end. Aggregation is a hollow-diamond line denoting a whole/part has-a relationship where the part can outlive the whole. Composition is a filled-diamond line denoting a strict whole/part relationship where the part’s lifecycle is owned by the whole. Generalization (a hollow-triangle arrow) models inheritance, realization (a dashed hollow-triangle arrow) models an interface being implemented, and dependency (a dashed arrow) means one class merely uses another temporarily, such as a method parameter.

  • Gives architects a shared visual vocabulary before code is written
  • Makes coupling and ownership explicit at design time
  • Surfaces multiplicity and cardinality constraints early
  • Speeds up onboarding by showing structure at a glance

AI Mentor Explanation

A class diagram of a cricket league draws Team and Player boxes connected by a filled-diamond composition line, because a Player’s squad membership is owned and destroyed together with the Team roster for that season. A plain association line connects Umpire to Match, since an umpire officiates many matches without being owned by any one of them. The diagram lets a selector see ownership and multiplicity — one Team, eleven Players — before a single line of scheduling code exists.

Step-by-Step Explanation

  1. Step 1

    Draw the classes

    Represent each class as a box with three compartments: name, attributes, operations.

  2. Step 2

    Pick the right relationship line

    Choose association, aggregation, composition, generalization, realization, or dependency based on the real coupling and lifecycle.

  3. Step 3

    Annotate multiplicity

    Add cardinality like 1, 0..1, or 1..* at each end of association-family lines.

  4. Step 4

    Validate ownership semantics

    Confirm filled diamonds only appear where the part truly cannot outlive the whole.

What Interviewer Expects

  • Correct distinction between association, aggregation, and composition
  • Knowledge of hollow vs filled diamond notation
  • Understanding of generalization vs realization arrows
  • Ability to map a real domain scenario onto the correct relationship

Common Mistakes

  • Using composition everywhere out of habit, ignoring true lifecycle ownership
  • Confusing the direction of the arrowhead for generalization vs realization
  • Omitting multiplicity annotations on association lines
  • Treating dependency and association as interchangeable

Best Answer (HR Friendly)

A UML class diagram is a visual map of a system before code exists — boxes are classes and the connecting lines tell you how those classes relate. A plain line means two classes just know about each other, a hollow diamond means one owns the other loosely, a filled diamond means strict ownership where parts die with the whole, and triangle arrows show inheritance or interface implementation.

Code Example

Composition vs aggregation reflected in code
class Engine {
    // Created and destroyed with its Car — composition
}

class Car {
    private final Engine engine; // owned; Engine has no life outside a Car
    Car() { this.engine = new Engine(); }
}

class Driver {
    // Not created by Car; can exist without any specific Car — aggregation
}

class Trip {
    private Driver driver; // reference to an independently-existing object
    Trip(Driver driver) { this.driver = driver; }
}

Follow-up Questions

  • What is the visual difference between aggregation and composition on a diagram?
  • When would you use a dependency arrow instead of an association?
  • How does generalization differ from realization in UML?
  • How do multiplicities like 0..1 and 1..* change the meaning of a relationship?

MCQ Practice

1. Which UML notation represents composition?

A filled diamond at the whole end denotes composition, where parts share the lifecycle of the whole.

2. A dashed line with a hollow triangle arrowhead pointing to an interface represents?

Realization shows a class implementing an interface, drawn as a dashed line with a hollow triangle.

3. What does a plain solid line without diamonds between two classes typically represent?

A plain solid line denotes a basic association — the two classes are structurally linked.

Flash Cards

Aggregation diamond?Hollow diamond — whole/part where the part can outlive the whole.

Composition diamond?Filled diamond — strict ownership; the part dies with the whole.

Generalization arrow?Solid line with a hollow triangle — represents inheritance.

Realization arrow?Dashed line with a hollow triangle — represents interface implementation.

1 / 4

Continue Learning