Unified Modeling Language (UML) is a widely used modeling language in software engineering. It is used to visualize, design, and document software systems. One of the most popular UML diagrams is the class diagram.
A class diagram is a graphical representation of the classes, interfaces, and their relationships in a system. It shows the static structure of the system and the objects that exist in the system. The class diagram is used to describe the attributes and methods of the classes and their relationships with other classes.
The class diagram consists of three main components: classes, associations, and multiplicity. A class is represented by a rectangle with the class name written inside it. The attributes and methods of the class are listed below the class name. Associations are represented by lines connecting two classes. The multiplicity of the association is shown at the ends of the line.
The class diagram is an important tool for software developers as it helps them to understand the structure of the system and the relationships between the classes. It is also useful for communication between developers and stakeholders as it provides a visual representation of the system.
In conclusion, the class diagram is a fundamental UML diagram that is used to represent the static structure of a system. It is an essential tool for software developers to design and document software systems.
Basic Elements
Element | Description |
---|---|
Class | A blueprint for creating objects that defines its attributes and methods |
Object | An instance of a class |
Attribute | A property of a class that describes its state |
Method | A function of a class that defines its behavior |
Association | A relationship between two classes |
Multiplicity | The number of instances of a class that can be associated with another class |
Inheritance | A relationship between a superclass and a subclass |
Polymorphism | The ability of an object to take on many forms |
Class Relationships
Relationship | Description |
---|---|
Association | A relationship between two classes where one class uses the other class |
Aggregation | A special form of association where one class is a part of another class |
Composition | A stronger form of aggregation where the part cannot exist without the whole |
Inheritance | A relationship between a superclass and a subclass where the subclass inherits the attributes and methods of the superclass |
Interface | A contract that specifies the methods that a class must implement |
Class Diagram Notation
Notation | Description |
---|---|
Class Name | The name of the class |
Attributes | The properties of the class |
Methods | The functions of the class |
Visibility | The access level of the attribute or method (public, private, protected) |
Abstract Class | A class that cannot be instantiated and must be subclassed |
Interface | A contract that specifies the methods that a class must implement |
Inheritance | A relationship between a superclass and a subclass |
Multiplicity | The number of instances of a class that can be associated with another class |
Association | A relationship between two classes |
Aggregation | A special form of association where one class is a part of another class |
Composition | A stronger form of aggregation where the part cannot exist without the whole |
Examples
Class Diagram for a Bank Account
+-----------------+
| BankAccount |
+-----------------+
| -balance: double|
+-----------------+
| +deposit(amount: double): void|
| +withdraw(amount: double): void|
+-----------------+
Class Diagram for a Car
+---------+
| Car |
+---------+
| -make: string|
| -model: string|
| -year: int|
+---------+
| +start(): void|
| +stop(): void|
+---------+
Class Diagram for a Library
+-----------+
| Library |
+-----------+
| -books: Book[]|
+-----------+
| +addBook(book: Book): void|
| +removeBook(book: Book): void|
+-----------+
+-------+
| Book |
+-------+
| -title: string|
| -author: string|
| -isbn: string|
+-------+
| +getTitle(): string|
| +getAuthor(): string|
| +getISBN(): string|
+-------+
Reference:
https://3dcitydb-docs.readthedocs.io/en/release-v4.1/3dcitydb/uml/