What are class diagrams?

Class diagrams are a type of UML (Unified Modeling Language) diagram used to visualise or represent the structure and relationships of classes within a system. This is often used to construct and visualize Object Oriented Systems.

Class diagrams can be summarised as boxes with arrows in between with text in those boxes. In theory this makes it so that you can create this in almost any kind of software, but there are a lot of different arrow types which makes it more difficult to do so. I personally use Draw.io.

Quote

Class diagrams provide a high-level overview of a system’s design, helping to communicate and document the structure of the software. They are a fundamental tool in object-oriented design and play a crucial role in the software development lifecycle.

Source: geeksforgeeks

Notations

Class

A class is simple, I can even show you it in obsidian without having to switch to some drawing software. This is because Obsidian supports Mermaid Diagrams.

---
title: Class example
---
classDiagram
    class ClassName
    ClassName : +String publicField
    ClassName : -Bool privateField
    ClassName : +publicMethod(string) string
    ClassName : -privateMethod(int) bool

In the above example we can see a simple example:

  • ClassName: This is the name of the Class or Interface. Whenever it is an Interface, you will put <<interface>> in front of / above the Classname.
  • +String publicField: The + or - symbols indicate whether the property / field is public or private. You don’t need to specify whether something is static etc, that will be left for the implementation to decide. Then you put the Datatype of the field, and lastly you put the name of your field.
  • +publicMethod(string) string: The + and - mean the same thing here as it does for the fields. Then you follow with the name of the method, in the above case it is called publicMethod. Then we follow with all parameters we want to pass into the method, you can decide whether you also want to name them (variablename: string) but I personally don’t since it will look cluttered to me. And then you follow it at the end with the : string, this is the return type of the method.

Relationships

There are a lot of possible relationship types between classes, I am not going to explain them all, but I will show you the picture that I always use whilst modelling my class diagrams. I advice you to visit geeksforgeeks if you want information on a specific relationship. To use these arrows correctly requires you to know these OOP concepts.

img I don’t know the source of this image because I have been using this for 3 years at the point of writing this.

Here is an example image of my class diagram from an old school project, it is cluttered but correct.

img

Go visit my friend Thomas his article for more info on this subject.