0% completed
Class Diagram
On This Page
The box
The relationships
The one test that decides most lines
How much detail to draw
Key Takeaways
The class diagram is the answer to an object oriented design question. Everything else in this chapter exists to get you to it or to explain it once it is drawn.
It shows the static structure of a design: what classes exist, what each one holds, what each one can do, and how they connect. It is also the only UML diagram that maps directly onto code. That is why it is the one an interviewer expects to see.
The box
A class is a rectangle in three parts. The name on top. The attributes in the middle. The operations at the bottom.
Two markers appear in front of a member:
-means private. Nothing outside the class can touch it.+means public. This is the class's interface to everything else.
An abstract class is a class you never intend to create an object from. It is written in italics, and so is an abstract operation. Nothing else about the box changes.
You do not have to fill in all three parts. Early in an interview a box with only a name is a perfectly good class diagram. Attributes and operations arrive as the design settles.
The relationships
This is the part that carries the design, and the part worth being precise about.
Association. A plain line. It means the two classes know about each other.
Add an arrowhead to make it one directional. Then only the class at the tail knows about the one at the head. In the picture above, Flight points at Aircraft, and the aircraft knows nothing about the flight.
Multiplicity. Numbers written at the ends of a line, saying how many instances take part. 1, * for many, 0..1 for optional, 2..4 for a range. In the picture, a FlightInstance has exactly two Pilots, and a pilot has many flight instances.
Aggregation. A hollow diamond at the whole end. The part can outlive the whole. An Airline aggregates Aircraft: shut the airline down and the aircraft still exist.
Composition. A filled diamond at the whole end. The part cannot outlive the whole. A Flight composes its WeeklySchedule: delete the flight and the schedule is meaningless.
Generalization. A solid line with a hollow triangle pointing at the parent. This is inheritance. Crew, Pilot and Admin are all a Person.
Realization. A dashed line with a hollow triangle pointing at the interface. An interface is a list of operations with no code behind them, and realization means implementing one. It looks like generalization on purpose, because it is the same idea with no code inherited.
Dependency. A dashed line with an open arrowhead. One class uses another without holding it, usually as a parameter or a return type. FlightReservation depends on Payment.
Those seven are the whole vocabulary. Here they are together, which is the same legend that sits inside every case study in this course.
The one test that decides most lines
Aggregation and composition look almost the same. Most candidates choose between them almost at random. There is a one sentence test.
Destroy the whole. Does the part still make sense on its own?
If yes, it is aggregation, a hollow diamond. A library that closes does not delete its members; a person exists either way.
If no, it is composition, a filled diamond. A parking lot that is demolished takes its floors with it; a floor of a lot that does not exist is nothing.
Say the test out loud when you draw the diamond. The interviewer then hears a decision instead of a guess.
How much detail to draw
A whiteboard is not a code generator. Aim for this:
- Every class gets a box and a name. Ten to fifteen boxes is a full answer.
- Attributes only where they matter. The two or three fields that carry the design, not every string.
- Operations only where they are interesting. The methods that appear in your flows.
- Every line gets a relationship type and a multiplicity. This is the part not to skip. An unlabelled line is a decision you have not made.
If you run short of time, drop attributes before you drop relationships. A diagram of named boxes with correct lines is a design. A diagram of fully specified boxes with vague lines is not.
Key Takeaways
- The class diagram shows what classes exist and how they connect. It is the only UML diagram that maps directly onto code.
- A class box has three parts,
-marks private and+marks public, and an abstract class is written in italics. - Association is knowing about, aggregation is holding, composition is owning, generalization is inheriting, realization is implementing, dependency is using.
- To choose between aggregation and composition, destroy the whole and ask whether the part still makes sense.
- Draw every box, but only the attributes and operations that matter. Never leave a line without a type and a multiplicity.
The class diagram says what the design is. It says nothing about what happens in what order. The next lesson covers the diagram that does: the activity diagram, which walks one flow from start to finish.
Nirbhay Lourembam
· 2 years ago
I feel "Generalization" should be replaced with "Implements and interface" ?
And maybe it should have been used as Inheritance / Generalization
Or does Generalization mean implementing an interface ? Because as per the definition it seems to suggest Inheritance and not implementing an Interface.
Or am I missing something here ?
kedi kebba
· 3 years ago
From this class diagram, it is depicted that the relationship between an Aircraft and Airline is Aggregation - and therefore an Airline and Aircraft can live independently. I would love to think about this logically, can an airline exist without an aircraft(s) ?
While am revising this, am trying to relate to real world examples so that I understand and better represent the concepts during my interview, so, ,in an interview, are we supposed to think about these situations logically or its all about an idea and demonstrating that you know the concepts and can apply them even if they don't fit in the interviewer's logical view as long as you can explain how you are seeing it from your perspective - because i understand there is not fit it all design solution.
Adrien
· 3 years ago
The description of uni-directional versus bidirectional association is confusing to me. You wrote:
• By contrast, in a uni-directional association, two classes are related - but only one class knows that the relationship exists. In the below example, only Flight class knows about Aircraft; hence it is a uni-directional association
But on the class diagram we see that Aircraft has a getFlights() method which seems to contradict this statement. Similarly the association between Airport and Flight is annotated as "Uni-directional" but we see that Airport has a getFlights() method and that Flight has some Airport-typed properties.
Reading Progress
0%
On This Page
The box
The relationships
The one test that decides most lines
How much detail to draw
Key Takeaways