UML Lesson
Class diagrams¶
Learning Objectives¶
- Know how to draw class diagrams and how to describe classes and their attributes, constructors, and methods
- Know how to describe connections between classes and describe inheritance and interface implementation
- Can implement a class based on a class diagram
A class diagram is a diagram used in designing and modeling software to describe classes and their relationships. Class diagrams enable us to model software in a high level of abstraction and without having to look at the source code.
Classes in a class diagram correspond with classes in the source code. The diagram shows the names and attributes of the classes, connections between the classes, and sometimes also the methods of the classes.
Next we will get familiar with creating and reading class diagrams using UML. Using a unified modeling language ensures that class diagrams drawn by different people can be read and understood by everyone familiar with ANY OOP language.
Describing class and class attributes¶
- First we will describe one class and its attributes.
- Below is the source code for a class called
Person
which has two class attributesname
andage
.
1 2 3 4 |
|
- In a class diagram, a class is represented by a rectangle with the name of the class written on top.
- A line below the name of the class divides the name from the list of attributes (names and types of the class variables).
- The attributes are written one attribute per line.
- In some class diagrams, class attributes are written “attributeName: attributeType”, but in the mermaid diagrams we’ll be using, it sticks to attribute type attribute name.
- A + before the attribute name means the attribute is public.
- A - means the attribute is private.
- Speak up now if you don’t recognize public/private access modifiers!
Describing class constructor¶
Below we have the source code for a constructor for our Person class. The constructor gets the name of the person as a parameter.
1 2 3 4 5 6 7 8 9 |
|
- In a class diagram, we list the constructor (and all other methods) below the attributes.
- A line below the attributes list separates it from the method list.
- Methods are written with +/- (depending on the visibility of the method), method name, types and their parameters.
- The constructor above is written
+Person(String initialName)
- The parameters are written the same way class attributes are — “parameterType parameterName”.
Describing class methods¶
Below we have added a method printPerson() which returns void to the Person class.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
- In a class diagram, we list all class methods including the constructors; constructors are listed first and then all class methods.
- We also write the return type of a method in the class diagram.
Info
A class diagram describes classes, constructors and methods
A class diagram describes classes and their attributes, constructors and methods as well as the connections between classes. However a class diagram tells us nothing about the implementation of the constructors or the methods. Therefore a class diagram describes the structure of an object but NOT its functionality.
For example the method printPerson
uses the class attributes name
and age
, but this cannot be seen from the class diagram.
Below we have added the method getName to the Person class which returns the name of the Person.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Section Exercise 1:¶
The class diagram below shows the class Customer. Implement the class in the exercise.
Answer
1 2 3 4 5 |
|
Section Exercise 2:¶
The class diagram below depicts the classes Book and Plane. Implement the classes in the exercise.
Answer
1 2 3 4 5 |
|
1 2 3 4 5 |
|
Connections between classes¶
In a class diagram, the connections between classes are shown as arrows. The arrows also show the direction of the connection.
Below we have a class Book.
1 2 3 4 5 6 |
|
If we add a variable author, type of which is Person, in the source code the variable is declared like all other class variables.
1 2 3 4 5 6 7 |
|
- In a class diagram variables which refer to other objects are not written with the rest of the class attributes.
- They are shown as connections between the classes.
In the class diagram below we have the classes Person and Book, and the connection between them.
- The arrow shows the direction of the connection.
- The connection above shows that a Book knows its author but a Person does not know about books they are the author of.
- We can also add a label to the arrow to describe the connection.
- In the above diagram the arrow has an accompanying label telling us that a Book has an author.
If a book has multiple authors, the authors are saved to a list.
1 2 3 4 5 6 7 |
|
- In a class diagram, this situation is described by adding a star to the end of the arrow showing the connection between the classes.
- The star tells us that a book can have between
0
andunlimited
number of authors. - Below we have not amended the label to describe the cardinality / multiplicity of the connection.
- Class methods are described just like we did before.
- Below we have added methods
getAuthors
andaddAuthor
to the Book class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Section exercise 1:¶
In the class diagram below, the classes Show
and Ticket
and their connection are depicted. The star is at the Ticket
end of the connection - in this case the star gives some extra information of the connection; even though a show doesn’t know about the tickets that have been sold to it, you can still sell many tickets to one show.
Implement the classes in the diagram in the exercise base.
Answer
1 2 3 4 |
|
1 2 3 4 5 |
|
- If there is no arrowhead in a connection, both classes know about each other.
- Below is an example where a book knows about its author and a person knows about a book they have written.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
- By default — if there is no star on the connection — the connection is singular.
-
The classes above are interesting, because a Person can only have one book.
-
If a person can have multiple books and a book can have multiple authors, we add a star to both ends of the connection:
- Now the person class would be as follows:
1 2 3 4 5 6 7 8 9 |
|
Section Exercise 2:¶
Two classes, Student and University, are depicted below, as well as the connection between them. Implement these classes in the exercise base.
Answer
1 2 3 4 5 |
|
1 2 3 4 |
|
Describing inheritance¶
- In a class diagram inheritance is described by an arrow with a triangle head.
- The triangle points to the class being inherited from.
In the below example the Engine
inherits the class Part
.
In the below example the ProductWarehouseWithHistory
class inherits the ProductWarehouse
class, which, in turn, inherits the Warehouse
class. ChangeHistory
is a separate class connected to the ProductWarehouse
. ProductWarehouseWithHistory
knows about the ChangeHistory
but the ChangeHistory
does not know about the ProductWarehouseWithHistory
.
- Inheritance of abstract classes is described almost the same way as regular classes. However, we add the description <<abstract>> above the name of the class.
- The name of the class and its abstract methods are also written in itallics.
Section Exercise 1:¶
The classes Player and Bot and the connection between them are depicted in the class diagram below. Implement these classes in the exercise.
Answer
1 2 3 4 5 6 7 8 9 10 11 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Describing interfaces¶
- In class diagrams, interfaces are written <<interface>> NameOfTheInterface.
- Below we describe an interface Readable.
1 2 3 |
|
- Methods are described just like they are for a class.
- Implementing an interface is shown as a dashed arrow with a triangle arrowhead.
- Below, we describe a situation where Book implements interface Readable.
Section exercise 1:¶
Below you’ll see the interface Saveable and the class Person. Implement the contents of this class diagram in the exercise base.
Answer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
How these should be drawn?¶
Class diagrams are an excellent way to describe a problem and a problem-domain to others. They are particularily useful when a programmer is designing a program with multiple classes.
Often a class diagram is drawn on a whiteboard or a large sheet of paper during the design phase. Class diagrams should be thought of as helpful tools to build a program, which can be thrown away afterwards. You should not use too much energy to think about the correctness and details of the modeling language. Class diagrams should also be drawn in a suitable level of abstraction. For example, if you have tens of classes, it might not be worth describing each attribute and each method of each class; getting a good overview of the program structure is the most important.
The class diagrams here have been drawn using mermaid.live. Some IDEs also have tools for drawing class diagrams; for example, easyUML draws class diagrams from the source code, IntelliJ has a mermaid plugin, and of course all of the AI Bots can be prompted to draw these diagrams.
Section exercise 1:¶
Below you’ll see a somewhat larger class diagram. In it are the classes A, B, C, D, and E, as well as the interfaces IA, IB, and IC. Create these classes and interfaces using ChatGPT.
Answer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
Section Exercise 2:¶
Use the service on the website mermaid.live to create a class diagram that represents the following classes. Check your textual representation used to create the diagram from the service to the answer below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
Answer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Composition VS Association
Finale Note:Association is a broad term for any link between two objects, while composition is a strong, exclusive part-of relationship where the contained object’s lifecycle is tied to the containing object. In association, both objects can exist independently, but in composition, the contained object cannot exist without its parent.