# UML class diagram

> Classes with attributes and methods, an association with multiplicities, and inheritance. A Software diagrams example with its LaTeX source.

A Software diagrams example in the TeXeR TikZ library: https://texer.org/library/software-diagrams/class-diagram

Package: [Software diagrams](https://texer.org/library/software-diagrams.md), on CTAN at https://ctan.org/pkg/pgf.

Tags: UML, class diagram, inheritance, object-oriented

## Source

```latex
\usetikzlibrary{shapes.multipart, arrows.meta, positioning}

\begin{tikzpicture}[
  node distance=12mm and 16mm,
  class/.style={
    rectangle split, rectangle split parts=3, draw, thick, font=\small,
    rectangle split part align={center, left, left}, align=left, minimum width=32mm,
  },
  inherit/.style={-{Triangle[open, length=3mm, width=3mm]}, thick},
]
  \node[class] (account) {\textbf{Account}
    \nodepart{second} $-$ id: int \\ $-$ balance: float
    \nodepart{third} $+$ deposit(amount) \\ $+$ withdraw(amount)};
    \node[class, left=of account] (customer) {\textbf{Customer}
      \nodepart{second} $-$ name: string
      \nodepart{third} $+$ accounts()};
      \node[class, below=of account, xshift=-19mm] (savings) {\textbf{Savings}
        \nodepart{second} $-$ rate: float
        \nodepart{third} $+$ addInterest()};
        \node[class, below=of account, xshift=19mm] (checking) {\textbf{Checking}
          \nodepart{second} $-$ overdraft: float
          \nodepart{third} $+$ writeCheck()};

          \coordinate (join) at ([yshift=-6mm]account.south);
          \draw[thick] (savings.north) |- (join) -| (checking.north);
          \draw[inherit] (join) -- (account.south);
          \draw[thick] (customer) -- node[above, pos=0.18] {1} node[above, pos=0.8] {1..*} (account);
\end{tikzpicture}
```

## As a complete document

The same example in a document that compiles on its own with pdfLaTeX:

```latex
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{amssymb}
\usetikzlibrary{shapes.multipart, arrows.meta, positioning}

\begin{document}

\begin{tikzpicture}[
  node distance=12mm and 16mm,
  class/.style={
    rectangle split, rectangle split parts=3, draw, thick, font=\small,
    rectangle split part align={center, left, left}, align=left, minimum width=32mm,
  },
  inherit/.style={-{Triangle[open, length=3mm, width=3mm]}, thick},
]
  \node[class] (account) {\textbf{Account}
    \nodepart{second} $-$ id: int \\ $-$ balance: float
    \nodepart{third} $+$ deposit(amount) \\ $+$ withdraw(amount)};
    \node[class, left=of account] (customer) {\textbf{Customer}
      \nodepart{second} $-$ name: string
      \nodepart{third} $+$ accounts()};
      \node[class, below=of account, xshift=-19mm] (savings) {\textbf{Savings}
        \nodepart{second} $-$ rate: float
        \nodepart{third} $+$ addInterest()};
        \node[class, below=of account, xshift=19mm] (checking) {\textbf{Checking}
          \nodepart{second} $-$ overdraft: float
          \nodepart{third} $+$ writeCheck()};

          \coordinate (join) at ([yshift=-6mm]account.south);
          \draw[thick] (savings.north) |- (join) -| (checking.north);
          \draw[inherit] (join) -- (account.south);
          \draw[thick] (customer) -- node[above, pos=0.18] {1} node[above, pos=0.8] {1..*} (account);
\end{tikzpicture}
\end{document}
```
