UML Class Diagrams: A Complete Guide
UML class diagrams explained
When constructing a house or building, architects and engineers draft a set of blueprints to visualize all aspects of construction before any work begins. Unified modeling language (UML) class diagrams serve the same function as a blueprint but for software development.
Just like you wouldn't build a house without a blueprint, it's not a great idea to build a system without a UML diagram during initiation and updating. UML class diagrams are part of a series of UML diagrams that visualize different aspects of development, all using the same language for easier interpretation.
Consider this guide as your UML class diagram cheatsheet. Learn the benefits of using them for conceptual design, how they work, and how to create a UML class diagram.
What is a class diagram in UML?
Class diagrams are one of several types of UML structure diagrams. Structure diagrams show the static structure of an system, rather than showing how an object changes over time.
Class diagrams visualize a system's classes and the relationships between them.
In object-oriented design, classes create and operate on objects. Objects are instances of classes. So classes are critical high-level elements of a system. They're derived during design and are used for communication about the design, or design changes.
In a class diagram, the class names are the same as the object names because a class's purpose is to define the attributes and operations for each object instance in the system. A class is a blueprint for an object, and a class diagram is the static blueprint of the system.
In software development, UML class diagrams don't exist on their own. They depend on use case diagrams and are closely tied to object and communication diagrams. Together, all UML diagrams model, conceptualize and document the workings of a system before, during, and after implementation.
What is a class in UML?
A class is a modeling element that defines the characteristics of the object it represents, including its attributes and behaviors.
Let's use the class ‘Car' as an example. 'Car' has a set of static attributes, like brand, model, year, and color. The class ‘Car' also has methods (things cars can do) like accelerating, decelerating, stopping, and starting. Since an object is an instance of the class: one such object instance can be: Subaru, Forester, 2023, dark green. In this example, the attributes of this car are make, model, year and color.
What is the purpose of UML class diagrams?
A UML class diagram has two main purposes as a static model of an object-oriented system:
To visualize a system's classes and their properties.
To show and analyze the relationships between the classes.
Aside from these, UML class diagrams are also the basis for component and deployment diagrams that show the hardware and software aspects of a system.
Benefits of UML class diagrams
UML class diagrams are practical modeling tools for building a software architecture. Along with other UML diagrams, developers and stakeholders visualize different views of a system. These diagrams help us understand how the system works, how it behaves, and how its parts relate.
Creating class diagrams during design facilitates the development process by clearly showing classes, their attributes, and their methods. They also show how the classes relate to each other. Seeing a conceptual build of a system before any code is written helps helps developers communicate with one another, and with other stakeholders.
Class diagrams also support changes to the system by showing a schematic of an entire application. Applying changes or additions to a UML class diagram provides a preview of any new classes and how the change impacts existing classes.
Key components of UML class diagrams
UML class diagrams contain classes and their interactions. Each class is shown in a rectangle with, from top to bottom, the class name, its attributes, and its methods. Only the class name is required. The level of detail needed determines whether the attributes and methods are shown.
Interactions in a UML class diagram show the relationships among the classes using lines and arrowheads. The arrowheads have specific meanings to differentiate among the types of relationships. These relationships include inheritance as well as bidirectional and unidirectional relationships. Classes can be grouped into packages of classes that are closely related.
UML class diagram notations
Basic notation in a UML class diagram includes the labels in the class members, their visibility (who can access them), and the eventual packages. When creating a UML class model, use the web app-approved font, Open Sans. Generally, UML class diagrams follow style conventions like camel case, where the second and subsequent words are capitalized, and there are no spaces between the words.
Classes
A class name reflects an object or entity name. The class name goes in the center of the top section, bold and capitalized. In the model, the class name section is the only mandatory section in a class. The other two are optional, depending on the diagram's objective and view.
Attributes
The middle section of the model includes the attributes which describe a class's static characteristics that'll apply to object instances defined by that class. Imagine we're building a class diagram to visualize the operation of an ATM. The DebitCard class attributes are card number and owner; each instance has its own number and owner.
Methods (operations and behaviors)
The third section of the class model is what the class "does." What are the actions that the object instance will be able to do? In the DebitCard class, the operation would be something like giving access.
Visibility
Plus and minus signs represent whether a class's attributes and operations are public (visible from anywhere within the system) or private (visible only within the class). The hashtag (pound sign) indicates protected visibility.
Package
A group of classes or a group of other packages are visualized as boxes with a label. The package name goes on the label and is capitalized. When using packages, classes don't need to have all three sections in view at all times.
UML class diagram relationship notations
UML class diagram models are connected visually using lines, textual notations, and multiplicity numerals. The following list covers the basic relationship notations needed to create a UML class diagram for your system.
All relationship notations, except for Realization, involve a name written next to the line and on the side of the class, it pertains to. Keep all text sizes balanced and lines orderly to achieve an easy-to-read diagram.
Association
An association connects two classes in any logical connection, is the most used relationship notation, and looks like a straight line. Associations are inherently bilateral, meaning both associated classes know that the other class exists. When the association is unilateral, it has a plain arrowhead that looks different from the triangular inheritance and realization arrow.
N-ary determines associations that connect more than two class models. The next N-ary association to binary is the ternary association. These notations are lines connected with an empty diamond at the conjunction.
Aggregation and composition
Aggregation and composition are subsets of the association relationship.
The aggregation relationship connects classes using a straight line and a hollow diamond shape next to the parent class. In aggregation, the child classes exist independently of the parent. If the parent class is deleted, the child class continues to exist.
Composition is the opposite. When two classes connect with a composition notation using a straight line and a filled diamond shape, they don't exist independently. When the parent class is deleted, so is the child.
Reflexive association
The reflexive association is the only unilateral association available in class diagrams. They're made up of a straight line that loops alongside the class model. Their purpose is to visualize a relationship between one class instance and another instance of the same class.
Inheritance and generalization
The inheritance or generalization notation visualizes a relationship between classes that inherit properties. An inheritance relationship is visualized with a straight line and a hollow triangle shape pointing at the independent class.
Dependency
Dependency notations are visualized with a staggered line and simple arrowhead identifying the relationship direction. Like the inheritance notation, the arrow points from the dependent class to its superclass. Changes that happen to the superclass will cause changes to the subclass.
Realization
A realization relationship connects a class that realizes or implements the behavior defined by another. The models in this relationship are called the supplier and client elements or superclass and subclass. The supplier is the source, and the client is the specification element. Realization notation is visualized with a staggered line and a hollow triangle shape.
Multiplicity
The multiplicity notation is a set of numbers placed at the intersection of a relationship line and a class model. The multiplicity of instances defines how many instances of an object participate in a relationship. The digits show the number of instances of one class that are linked to one instance of another class. They're denoted like this:
0..1 = Zero or one
1 = One only
0.. = Zero or more
1..* = One or more
3 = Three only
0..5 = Zero to Five
5..15 = Five to Fifteen
Creating a UML class diagram
Now it's time to create a UML class diagram of your own. Of course, you can always diagram from scratch. If you're new at creating UML diagrams, your best bet is to use a pre-designed template with all the model shapes and notation options. Miro has a UML class diagram template that works as a base for any UML class diagram you need to make.
Using Miro, or another diagram tool, follow this step-by-step tutorial on making a custom class diagram to visually conceptualize and model a system.
Define the classes
To define these objects/classes, write a use-case scenario for your system and extract the nouns from the statement. A Customer inserts their DebitCard into the ATM and withdraws Cash. The bold nouns are the objects and, therefore, also the classes.
Create a three-part model for each class and add the names to the top section. You can access pre-built class shapes in Miro through the diagramming tab on the left. Build the models but don't connect anything yet until you have all the classes.
Describe the class attributes
In the second compartment of each class model, add the attributes. The attributes of the ATM class model are the location, machine ID, and bank that manages it. So far, the ATM class with the attribute list looks like this:
ATM
+location : address
+machineId : integer
+managedBy : string
Add the class methods (operations)
In the third compartment, add the operations for each class in a list, like the attributes. For the ATM class, an operation could be that it reads the card, identifies a transaction, and asks for a pin. Now the class model looks like this:
ATM
+location : address
+machineId : integer
+managedBy : string
+readsCard
+identifiesTransaction
#asksForPin
Visualize the relationships and multiplicity
Arrange the class models on your Miro board to start associating them and visualizing relationships. Choose the type of line you want from the objects panel and select the line style and line ending according to the relationship.
Add the roles and multiplicity numerals along the lines by clicking the +T button in the objects menu. Adjust their position and size accordingly.
Keep it alive
UML class diagrams aren't maps set in stone — they are living mechanisms that help you visualize a system at any point from initiation to implementation and beyond. When you create a UML class diagram with Miro, you can always come back to it and revise any changes before changing any code in the system.