Reading & Drawing UML Class Diagrams
You'll learn to
- -Read and draw association, aggregation, composition, generalization, and dependency relationships correctly
- -Translate a verbal requirements list into a first-draft class diagram, the way an interview whiteboard actually demands
You will not be graded on producing a textbook-perfect UML diagram in an interview, but you are expected to communicate relationships between classes clearly and consistently - and that vocabulary comes from UML. Five relationship types cover almost everything you will need: association, aggregation, composition, generalization, and dependency.
The Five Relationships
- -Association: a general "uses" or "knows about" relationship between two classes - a Teacher and a Student are associated, but neither owns the other.
- -Aggregation ("has-a", weak ownership): a whole contains parts, but the parts can outlive the whole - a Department has Employees, but deleting the Department does not delete the Employees.
- -Composition ("has-a", strong ownership): a whole owns its parts exclusively, and the parts die with the whole - an Order owns its OrderLineItems; delete the Order and the line items go with it.
- -Generalization ("is-a"): inheritance - a DebitCard is a Payment.
- -Dependency ("uses briefly"): one class uses another temporarily, often as a method parameter or local variable, without holding a lasting reference - an OrderProcessor might depend on a TaxCalculator passed into a single method call.
Composition vs. Aggregation, the Interview-Relevant Test
The distinction candidates blur most often is aggregation vs. composition, and the test that actually resolves it is lifecycle ownership: if the part cannot meaningfully exist without the whole, and the whole is responsible for creating and destroying it, that is composition. If the part has an independent lifecycle and could belong to a different whole (or no whole at all), that is aggregation. A Car has an Engine (composition - the engine is built with the car and scrapped with it); a Car has Passengers (aggregation - passengers exist before, during, and after being in this particular car).
From Requirements to a First-Draft Diagram
The practical whiteboard technique: underline every noun in the requirements (candidate classes), underline every verb (candidate methods), then draw a relationship arrow for each sentence that connects two nouns. "A Library has many Books, and a Member can Borrow a Book" becomes: Library o-- Book (aggregation, books can exist without a specific library record), and a dependency or association line from Member to Book via a Loan class that represents the borrowing relationship itself as a first-class object.
First-draft class diagram from the Library requirements above
When a relationship between two classes has its own attributes (a Loan has a due date; a Booking has a price), that is usually a sign the relationship deserves to be its own class rather than staying an unlabeled line on the diagram - exactly why Loan is its own box above instead of a bare line from Member to Book.
How would you represent the relationship between a Car and its Engine versus a Car and its Passengers on a class diagram?
"Both are just associations - the car is connected to both the engine and the passengers."
"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."
What is the key test that distinguishes composition from aggregation?