Skip to content
LLD Learn/OOP Foundations & UML
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Reading & Drawing UML Class Diagrams

4 min read

You'll learn to

  • -Read and draw the five relationships: association, aggregation, composition, generalization and dependency
  • -Use one test, who owns the part's life, to tell aggregation from composition
  • -Turn a short written requirement into a first-draft class diagram, the way a whiteboard demands

Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off. Highlights are saved on this device only. Log in to save them to your account instead.

UML stands for Unified Modeling Language, a standard way of drawing software on paper. A class diagram is its most common drawing: each class is a box, and lines between boxes show how the classes are related. You will not be marked on textbook-perfect UML in an interview. You are expected to show the relationships clearly and consistently, and that vocabulary comes from UML.

An Analogy: Who Is Connected to Whom

Think of a map of a small company. Some links are loose: two colleagues know each other. Some are belongs-to links: a person is in a department but would still exist if the department closed. Some are all-or-nothing: an office desk exists only as part of its office. And some are kind-of links: a manager is a kind of employee. A class diagram has a line style for each of these.

The Five Relationships

  • -Association: a general "uses" or "knows about" link, drawn as a plain line. A teacher and a student are associated, and neither owns the other.
  • -Aggregation (a hollow diamond on the owner's end): the whole holds parts that can live without it. A department has employees, but closing the department does not delete the employees.
  • -Composition (a filled diamond): the whole owns the parts, and they die with it. An order owns its line items. Delete the order and its line items go too.
  • -Generalization (an arrow with a hollow triangle, pointing to the parent): the "is-a" link, which is inheritance. A debit card is a payment.
  • -Dependency (a dashed arrow): one class uses another briefly, often as a method parameter, without holding on to it. An order processor may use a tax calculator passed into one call.

Composition Versus Aggregation: The Lifecycle Test

Candidates blur aggregation and composition most often. The test that settles it is lifecycle ownership. Ask: if the whole disappears, does the part still make sense? If not, and the whole creates and destroys it, that is composition. If the part has its own life and could belong to another whole or to none, that is aggregation.

  • -A Car and its Engine: composition. The engine is built with the car and scrapped with it.
  • -A Car and its Passengers: aggregation. Passengers exist before, during and after the trip.
  • -An Order and its OrderLineItems: composition. A line item with no order is meaningless.
  • -A Playlist and its Songs: aggregation. Deleting a playlist must not delete the songs.

From Requirements to a First-Draft Diagram

The practical whiteboard technique has three moves. Underline every noun in the requirements, because each is a candidate class. Underline every verb, because each is a candidate method. Then draw a line for each sentence that joins two nouns. Try it on this requirement: "A library has many books. A member can borrow a book for 14 days, and we must know each loan's due date."

  • -Nouns: library, book, member, loan, due date. A due date is just a piece of data, so it will become a field.
  • -Verbs: borrow, return. Both involve a member and a book at a particular time.
  • -Lines: a library has many books (aggregation, because a book does not vanish when a library record is removed), and a member borrows a book.

That last line hides a decision. The borrowing itself has its own data, the due date. When a relationship has attributes of its own, it usually deserves to be a class, so we add Loan. The diagram below shows the result.

Library
Member
Loan
Book

First-draft class diagram from the Library requirements above

LibraryBook- aggregation: has many (books outlive any one library record)MemberLoan- createsLoanBook- references the borrowed book

When a relationship between two classes carries its own data (a Loan has a due date, a Booking has a price), make the relationship its own class. That is why Loan is a box above and not a bare line from Member to Book.

Interview Signal

How would you represent the relationship between a Car and its Engine versus a Car and its Passengers on a class diagram?

Weak Answer

"Both are just associations - the car is connected to both the engine and the passengers."

Strong Answer

"Car and Engine is composition: the engine is created with the car, has no independent lifecycle, and is destroyed with the car. Car and Passenger is aggregation: passengers exist independently, can move between cars, and outlive any single trip. The filled diamond goes on Car for the engine relationship; the hollow diamond goes on Car for the passenger relationship."

Check Yourself1 / 5

What is the key test that distinguishes composition from aggregation?

Discussion0

Join the Discussion

Sign in to leave comments, reply to others, or like insights.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!