Introduction to Class Diagram
Tags: oop, beginners
Published: 2021-10-19 09:19:52

There are many steps when developing a software. One of those steps is software design. In software design step, there are many diagrams that used and one of those is class diagram.

## Class Diagram

Class diagram is a diagram that represents relation between many entities that involved in a software. The class diagram consist of attribute and behavior. The attribute represented as a field and behavior represented as a method. This is the structure of class diagram.

![Class Diagram Structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aitz6pft00w40h5f0d4x.png)

This is the scope notations that can be used in fields or methods inside class diagram.

| Symbol | Scope     | Description                                                        | Example            |
| ------ | --------- | ------------------------------------------------------------------ | ------------------ |
| `+`    | public    | Field or method is available publicly                              | `+setName()`       |
| `-`    | private   | Field or method only available for its own class                   | `-id: String`      |
| `#`    | protected | Field or method is available for its own class and its sub classes | `#walk()`          |
| `~`    | package   | Field or method is available in a same package                     | `~getData(): Data` |

This is the example of class diagram that represents book entity.

![Book Entity](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1m6yg621akb2ukkwdcka.png)

Based on the class diagram above, there are three attributes or fields in a book including `id`, `title` and `author`. These three fields has a private scope. In `Book` class there is a one method called `getData()` that has a return type is a book data (`Book`).

## Relation in Class Diagram

There are many relationship types in class diagram including generalization, association, aggregation and composition.

### Generalization

This relationship is used to define an inheritance mechanism in class diagram. In this example, the generalization relationship is defined between `Vehicle` class and `Car` class.

![Generalization Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2mv4r992z4yskvjv8vhj.png)

### Association

This relationship is used to define a single relationship between classes that involved each other. In this example, the association relationship is used by `User` class and `Blog` class.

![Association Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dhw2vs5fb5n025g7sics.png)

### Aggregation

This relationship is used to define a relationship for a class that is a part of the another class. In this example, the aggregation relationship is used by `Car` class and `Machine` class.

![Aggregation Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/456rh1svl7s3f66c56w8.png)

### Composition

This relationship is used to define a relationship between classes that involved each other and cannot be separated. In this example, the composition relationship is used by `Course` class and `Teacher` class.

![Composition Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmbfpc6b7w53wuubt3lf.png)

## Create a Class Diagram

There are many tools that can be used to create a class diagram like [StarUML](https://staruml.io), [Draw.io](https://app.diagrams.net) and other software tools.

These are the steps to create a class diagram:

1. Define some entities that involved in a software or application. The entity could become concrete entitiy like book, user or abstract entity like course that taken by student.

2. Define the attributes and behaviors of an entity. Attributes described as a fields in class whilst behaviors described as a methods in class.

3. Define the scope for the fields and methods. In general, the scope for the fields is private and scoe for the methods is public or protected.

4. Define the relationships between classes.

5. Review the class diagram that already created to make sure the diagram is created correctly. The diagram must be correct in terms of syntax and semantics.

> Correct in terms of syntax means that a diagram is defined with the correct notation or symbol.

> Correct in terms of semantic means that a diagram is understandable.

## Notes

- Class diagram can be used to define the relationship between entities that involved in a software.

- There are many relationships that can be used in class diagram.

- Generalization can be used to define inheritance relationship between classes.

- Association can be used to define single relationship between classes.

- Aggregation can be used to define relationship for a class that is a part of the another class.

- Composition can be used to define relationship between classes that involved each other and cannot be separated.

## Sources

- Check out this blog post about [relationships in class diagram](https://creately.com/blog/diagrams/class-diagram-relationships/#Association).

I hope this article is helpful to understand the concept of class diagram. If you have any thoughts or comments you can write in the discussion section below.