# Class Diagram

In 
Published 2022-12-03

This tutorial explains to you what a UML Class Diagram is. Here are the concepts explanation and an example.

A Class Diagram in the Unified Modeling Language (UML) is a type of static structure diagram that describes the structure of a system by showing the system's classes, their attributes, operations (or methods), and the relationships among classes.

A Class (like in Java) is the template (the definition) which is used to create objects.

Here is the structure of a class:

UML provides mechanisms to define a class, such as attributes and methods and additional information about them.

To specify the visibility of a class attribute or method, these notations must be placed before the name of the method or attribute:

Sign Meaning Effect
+ Public Everyone can see it.
- Private Only the class in which it is declared can see it (It is private to the class).
# Protected Visible in this Class, Package, Subclass only.
~ Package Can only be seen and used by the package in which it was declared. This is the default in Java.

Here are the most used UML relationships among the classes:

  1. Association

Association relationship says that the 2 classes are related (can be associated). This is the most used relationship between classes.

Here is an example:

The meaning is "An employee is associated to (can work in) 0..1 departments". One department can have 0..n employees. Can be employees without a department associated (a new employee for instance).

You can use a line or an arrow.

  1. Dependency (is unidirectional - a dashed arrow must be used)

Sometimes there are some relations between classes. Because a dependency relationship can represent several types of relationships, keywords or stereotypes show the precise nature of the dependency.

Here are some useful keywords: «abstraction», «derive», «refine», «trace», «use», «call», «create», «instantiate», «send».

Here is an example:

That means that the CarFactory Class depends on Car Class. That is because CarFactory Class requires Car Class for its definition.

  1. Aggregation (a "has a" variant of association)

Here is an example:

A Pond has zero or more Ducks, and a Duck has at most one Pond (at a time). It is NOT necessary for a Pond to have a Duck => Aggregation.

  1. Composition (a "has a" variant of association, but stronger than "Aggregation")

A Book MUST HAVE 1 or more Pages. A Page is associated with a Book only.

  1. Inheritance (Generalization)

The specific classifier inherits part of its definition from the general classifier. The general classifier is at the arrow end of the connector. Attributes, associations, and operations are inherited by the specific classifier. Use the Inheritance tool to create a generalization between two classifiers.

Here is an example:

That means: Books and Computers are Inventory Items. A Book or a Computer will get the attributes and the methods of InventoryItem as well.

  1. Realization (Implementation)

Here is an example:

There are Cat and Horse objects, but no Animal object. Animal interface defines some attributes and methods for Cat and Horse classes. Cat and Horse Classes implements Animal interface.