How to use a UML Class Diagram

The Inquisitive Dev
6 min readNov 22, 2020

What is UML and what is it used for?

This article discusses one of the most commonly used Unified Modelling Language (UML) diagrams amongst developers, the Class Diagram. After a brief overview of why UML is useful and the various categories available, I will cover the structure and the relationships it can represent. I will end with a couple of example diagrams.

Throughout the engineering world, modelling is common place and is critical to the construction of successful systems. A model represents a simplification of reality. It helps you to see what is happening at various levels of abstraction. UML to software engineering is what architectural blueprints are to construction. It is used to convey system design and behaviour. UML is not a programming language, it is a general purpose modelling language that can benefit us in a number of ways:

  • Effectively and succinctly communicates the structure and intended behaviour of an application.
  • Helps all stakeholders to understand and visualise the system.
  • Highlights areas within the system which can be improved and/or simplified.
  • Helps engineers communicate complex concepts in a simple way.
  • Helps non-technical business people understand the system.
  • Serves as a guide for engineers to refer to during the build.

This article was originally posted over on my personal blog, The Inquisitive Dev. For more great content like this, head over there right now and subscribe.

Types of UML Diagrams

UML can be split into two categories.

Structural — Concerned with the static structure of a system. Represented using objects, attributes, operations and relationships. Example: Class diagrams.

Behavioural — A dynamic representation of a system at runtime, showing flows, behaviours and state changes within a system.

Type of UML diagrams split into structural and Behavioural
Type of UML diagrams split into structural and Behavioural

For a brief overview of what each of these UML types entail check out this guide by Creately.

UML Class Diagram notations

A class encapsulates state (attributes) and behaviour (operations). A UML class diagram, like the one shown below, helps us to visualise this structure.

Annotated class diagram
Annotated class diagram

A class diagram is split into three partitions.

In the first partition we have the class name. In the second partition we have the class attributes, including the attribute name, data type and access modifier. In the third partition are the class operations. Details include access modifiers, operation names, parameters, returns and data types.

Abstract Classes and Interfaces in UML

In UML class diagrams, abstract classes are represented in exactly the same way as regular classes, except the class name is in italics. Interfaces are declared using the <<interface>> notation. To learn more about the concept of abstraction in object oriented programming click here.

Representing abstract classes and interfaces in UML
Representing abstract classes and interfaces in UML

UML Class Diagram Relationships

We can use UML to represent a number of different relationships between classes. I have covered some of the more common ones below.

Inheritance (aka Generalisation)

Represents relationship an ‘is a’ relationship where a child class inherits attributes and operations from the parent class. This type of relationship uses a solid line with a white arrow pointing towards to source of the inheritance and is referred to as generalisation in UML terms. In the example below the Guitar inherits from Instrument because a Guitar ‘is a’ type of instrument.

Inheritance
Inheritance

I covered inheritance in detail in a previous post.

Association

Represents a relationship between two classes. This could be a direct relationship, where one class is an attribute of another. For example, we could have a Car class and a Passenger class. The Passenger class is an attribute of the Car class because a Car can have Passengers.

Association
Association

The number you can see above the line is what is known as multiplicity. In this case it tells us how many Passengers a Car can have. (0..* means between 0 and many)

Realisation

Realisation is a relationship between two classes where the implementing classes functionality is defined in another class. Realisation is represented by a dotted line with a white arrow pointing towards the implementing class. In the example below, the printing preferences that are set using the PrinterSetup interface are being implemented by the Printer.

Realisation
Realisation

Interfaces in object oriented programming are represented in this way. For more detail, see my previous post on abstraction.

Aggregation

A relationship where one class (the container class) is aggregated or built as a collection of another class (the contained class).

In the example below the Library class is made up of one or more Book classes. In aggregation, there is a weak relationship between the contained class and the container class. So in our example, Books will still exist if the Library were to disappear. We represent aggregation with a solid line between the two classes with a white diamond at the container class.

Aggregation
Aggregation

Composition

A composition relationship is very similar to aggregation. The difference is that in this relationship, the contained class is dependent on the existence of the container class.

The example below shows a composition relationship between the House class and the brick class. This is a strong relationship, as a single house is composed of many bricks. You can think of it as the House ‘owns’ the bricks. If the House is destroyed so too are the bricks.

The relationship is represented by a solid line with a black diamond at the container class.

Composition
Composition

UML Class Diagram Examples

Using UML class notation we can visually represent all sorts of object oriented software systems. From simple design patterns to entire applications.

The singleton design pattern
The singleton design pattern
An example of how a more complex system can be modelled
An example of how a more complex system can be modelled

Subscribe

If you found this article helpful I’d love you to become a subscriber to my blog. I’ll be posting all sorts of interesting content on a weekly basis. This blog is pretty new and I’d massively appreciate the signal of support. Please follow the link below to subscribe.

Thanks for reading.

--

--