0/0
UML & Documentation

UML Class Diagram Relationships

Master the six relationship types that connect classes in UML diagrams β€” from the weakest dependency to the strongest composition. Learn to read and draw every line style, arrowhead, and diamond, and understand multiplicity notation that describes "how many."

Explore Topics

πŸ’¨
Dependency
Temporary "uses-a" β€” the weakest relationship
πŸ” Relationship Spectrum β€” Weakest to Strongest
WEAK
STRONG
πŸ’¨
Dependency
Temporary "uses-a" β€” the weakest relationship

Dependency is the weakest UML relationship. It means one class temporarily uses another β€” typically as a method parameter, local variable, or return type. The dependent class does NOT store a reference as a field; the usage is confined to a single method scope. A change in the depended-upon class may affect the dependent class, because it relies on its interface. In UML, dependency is drawn as a dashed line with an open arrowhead pointing from the dependent class to the class it depends on.

Key Ideas
  • Weakest relationship β€” temporary usage, not permanent storage
  • UML notation: dashed line with open arrowhead (- - - ->)
  • Always directed: Class A depends on Class B (A - - -> B)
  • Caused by method parameters, local variables, or return types
  • No field reference β€” the object is used and released within method scope
  • A change in Class B may break Class A (A depends on B's interface)
Strengths
  • ✦Keeps coupling minimal β€” classes are loosely connected
  • ✦Easy to refactor since the relationship is not structural
  • ✦Makes the "uses" intent explicit in the diagram
  • ✦Good for utility and helper class relationships
Weaknesses
  • β–³Often overlooked in diagrams because it is so lightweight
  • β–³Students frequently confuse dependency with association
  • β–³Too many dependencies on one class signals a design smell
  • β–³Hard to track in large codebases without tool support
Used in:A Driver class that takes a Car parameter in its drive() method has a dependency on Car. The Driver does not own a Car β€” it borrows one temporarily. In a ride-sharing app, the DriverService depends on PaymentGateway only during processPayment().
Code Snippets

Quiz

Dependency Quiz
QUIZ 1/6
class Printer {
    void print(Document doc) {
        System.out.println(doc.getText());
    }
}
Which of the following Java code patterns represents a UML dependency relationship?

Diagrams

All Six UML Relationships at a Glance
Loading diagram...
Association vs Aggregation vs Composition
Loading diagram...
House Multiplicity Example
Loading diagram...

Practice

Classify each property into the correct UML relationship type:
EXERCISE 1/5
Dashed line with open arrowhead
Temporary usage in method scope
Solid line with unfilled triangle
Filled diamond on the whole side
Parts have independent lifetimes
Dashed line with unfilled triangle
Hollow diamond on the whole side
"implements" keyword in Java
Dependency
Generalization
Realization
Aggregation
Composition
UML Relationship Myths
EXERCISE 2/5
Dependency stores a reference as a field in the dependent class.
A class can implement multiple interfaces (multiple realization) in Java.
In aggregation, destroying the whole also destroys the parts.
The diamond (hollow or filled) always points toward the "whole" class.
Generalization and realization use the same arrowhead (open triangle) β€” the line style differs.
Association multiplicity "0..*" and "*" mean different things.
Complete the Java Relationship Code
EXERCISE 3/5
class Printer { // Dependency: temporary usage void print( doc) { System.out.println(doc.getText()); } } class Dog Animal { // Generalization: inheritance } class PayPalProcessor PaymentService { // Realization: interface implementation } class House { // Composition: owns rooms exclusively private List<Room> rooms; }
Spot the Wrong UML Notation
EXERCISE 4/5
Click on the line you think contains the bug, then press Confirm.
1Class Diagram Relationships:
2
31. Driver uses Car temporarily:
4 Driver β€”β€”β€”β€”β€”β€”β€”> Car (solid line + arrow)
5
62. Dog inherits from Animal:
7 Dog β€”β€”β€”β€”β€”β€”β€”β–· Animal (solid line + triangle)
8
93. House owns Rooms exclusively:
10 House β—†β€”β€”β€”β€” Room (filled diamond)
Classify each real-world example as Association, Aggregation, or Composition:
EXERCISE 5/5
House has Rooms (rooms destroyed with house)
Team has Players (players exist independently)
Student enrolled in Courses (bidirectional)
Invoice has LineItems (items meaningless alone)
Playlist has Songs (songs survive deletion)
Teacher works at School (both exist independently)
Association
Aggregation
Composition

References

πŸ“„

Baeldung: Association, Aggregation, and Composition in Java

baeldung.com

article
πŸ“„

Visual Paradigm: UML Aggregation vs Composition

visual-paradigm.com

article
πŸ“‹

UML-Diagrams.org: Multiplicity and Collections

uml-diagrams.org

docs
πŸ“„

GeeksforGeeks: Relationships in UML Diagrams

geeksforgeeks.org

article
πŸ“„

Composition over Inheritance β€” Wikipedia

en.wikipedia.org

article