What is a Class Diagram?
A class diagram is a static structure that is used in software
engineering. A class diagram shows the classes, attributes,
operations, and the relationship between them. This helps
software engineers in developing the code for an application. It is
also used for describing, visualizing, and documenting different
facets of a system.
Class diagrams are the only UML diagrams that can be mapped
directly with object-oriented languages. That is why they are
frequently used in the modeling of object-oriented systems and
are widely used during the construction of object-oriented
systems.
Class diagrams are one of the most important diagrams in coding
as they form the basis for component and deployment diagrams
and describe the responsibilities in a system. Along with that, they
are used for the analysis and design of an application and are
also used in forward and reverse engineering.
Class Notation
There are three major parts of a class diagram as shown in the
image below:
1. Class Name
2. Class Attributes
3. Class Relationship
A single rectangle is used to represent the class as shown above.
The rectangle is divided into three compartments with the
topmost being Class Name, then Attributes in the middle, and
Operations in the bottom.
Class Name
The class name is important for graphical representation. It
should be written in bold in the top compartment and start with a
capital letter. Moreover, an abstract class should be written in
italics.
Attributes
Attributes are written in the middle compartment and list down all
the properties of the object being modeled. You can simply add
new attributes or derive new attributes from already listed
attributes. Attributes must be meaningful and are usually used
with the visibility factor that describes the accessibility of an
attribute.
Operations
Operations are processes that a class knows to carry out. They
correspond to the methods of a class. You don’t need to show
operations that are similar to attributes because one can already
deduce that from the information.
Class Relationships
To create a class diagram, the next step is building relationships.
There are three main types of relationships here:
1. Generalizations
2. Associations
3. Dependencies
Generalizations
Generalizations are often known as Inheritance because it links a
subclass to its superclass. The class diagram allows a subclass
to inherit from multiple superclasses but it can’t be used to model
interface implementation. Checking, Savings, and Credit Accounts
are generalized by Account
Associations
Association shows a static relationship between two entities. The
association between a student and school is “studies”.
The multiplicity factor in association represents how many times
an attribute is multiplied. If 100 people work at an organization,
then the attribute has multiplied 100 times.
In aggregation, 2 classes have a whole-part relationship. For
example, if an employee does not come, the organization will
remain there.
Dependencies
Dependency shows that one class depends on another. Change in
one class will create change in another class. For example, an
employee is dependent on the organization.
Class Diagram Examples of Common Scenarios
1. Class Diagram for ATM
This class diagram for the atm maps out the structure and
attributes of how an ATM works. It also shows the relationship
between multiple classes. You can use this template as it is or
modify it according to your needs.
2. Class Diagram for Hotel Management System
3. Class Diagram for Library Management System
4. Class Diagram for Online Shopping
5. Class Diagram for Hospital Management System
6. Class Diagram for Banking System
7. Class Diagram for Student Registration System