Class Diagram Tutorial
Class diagrams are easier than they look. They're simply diagrams that describe the structure of a system by modeling its classes, attributes, operations, and relationships between objects.
To create a class diagram of your own, just click the buttons below. Lucidchart is free and easy to use; plus, you can collaborate with an unlimited number of teammates!Demo now Sign up free
What is a Class Diagram in UML?
The Unified Modeling Language can help you model various subsets of diagrams, including behavior diagrams, interaction diagrams, and structure diagrams. Class diagrams are a type of structure diagram because they describe what must be present in the system being modeled. They are typically used by engineers to document software architecture.
A class diagram is at the heart of UML. It represents the core purposes of UML because it separates the design elements from the coding of the system. UML was set up as a standardized model to describe an object-oriented programming approach. Since classes are the building block of objects, class diagrams are the building blocks of UML. The diagramming components in a class diagram can represent the classes that will actually be programmed, the main objects, or the interaction between class and object.
The class shape itself consists of a rectangle with three rows. The top row contains the name of the class, the middle row has the attributes of the class, and the bottom section expresses the methods or operations that the class may utilize. In a diagram, classes and subclasses are grouped together to show the static relationship between each object. The UML shape library in Lucidchart can help you create nearly any custom class diagram using our UML diagram tool.
Basics of Class Diagrams
Class - Diagram Anatomy
The class diagram is composed of three parts:
- Upper section - Name of the class - This section is always required whether you are talking about the classifier or an object.
- Middle Section - Attributes of the class - The attributes describe the variables that describe the qualities of the class. This is only required when describing a specific instance of a class.
- Bottom section - Class operations (methods) - Displayed in list format, each operation takes up its own line. The operations describe how a class can interact with data.
Member Access Modifier
All classes have different access levels depending on the access modifier (visiblity). Here are the following access levels with their corresponding symbols:
- Public (+)
- Private (-)
- Protected (#)
- Package (~)
- Derived (/)
- Static (underlined)
There are two scopes for members: classifiers and instances. Classifiers are static members while instances are the specific instances of the class. If you are familiar with basic OO theory, this isn't anything groundbreaking.
Object / Class Interactions in Class Diagrams
Interactions between objects and classes are an integral part of class diagrams.
Inheritance is when a child object assumes all the characteristics of its parent object. For example, if we had the object vehicle, a child class Car would inherit all the attributes (speed, numbers of passengers, fuel) and methods (go(), stop(), changeDirection()) of the parent class in addition to the specific attributes(modelType, # of doors, autoMaker) and methods of its own class (Radio(), windshieldWiper(), ac/heat()). Inheritance is shown in a class diagram by using a solid line with a closed, hollow arrow.
Bidirectional associations are the default associations between two classes and are represented by a straight line between two classes. Both classes are aware of each other and of their relationship with each other. In the example above, the Car class and RoadTrip class are interrelated. At one end of the line the Car takes on the association of "assignedCar" with the multiplicity value of 0..1 which means that when the instance of RoadTrip exists, it can either have one instance of Car associated with it or no Cars associated with it. In this case, a separate Caravan class with a multiplicity value of 0..* is needed to demonstrate that a RoadTrip could have multiple instances of Cars associated with it. Since one Car instance could have multiple "getRoadTrip" associations-- in other words, one car could go on multiple road trips--the multiplicity value is set to 0..*
A unidirectional association is drawn as a unbroken line with an open arrowhead pointing from the knowing class to the known class. In this case, on your road trip through Arizona you might run across a speed trap where a speed cam records your driving activity, but you won't know about it until you get notification in the mail. It isn't drawn in the image but in this case the multiplicity value would be 0..* depending on how many times you drive by the speed cam.
Class Diagram Applications
Class diagrams have a number of benefits for any organization. Try using them to:
- Illustrate data models for information systems.
- Understand the general overview of an application's schematics.
- Express the needs of a system and disseminate that information throughout the business.
- Create detailed charts that focus on the programming code needed to implement the described structure.
- Provide an implementation-independent description of types used in a system and passed between its components.
Class Diagram Components
Depending on the context, classes in a class diagram can represent the main objects, interactions in the application, and classes to be programmed.To answer the question of "What is a class diagram in UML?", you should first understand its basic makeup.
- Classes - a template for creating objects and implementing behavior in a system. In UML, a class represents an object or a set of objects that share a common structure and behavior. They're represented with a rectangle that includes rows of the class name, its attributes, and its operations. When drawing a class diagram on a class diagram, only the top row must be filled out—the others are optional if you'd like to provide more detail.
- Name - the first row in a class shape.
- Attributes - the second row in a class shape. Each attribute of the class is displayed on a separate line.
- Methods - the third row in a class shape. Also known as operations, these are displayed in list format with each operation on its own line.
- Signals - symbols that represent one-way, asynchronous communications between active objects.
- Data types - classifiers that define data values. Data types can model both primitive types and enumerations.
- Packages - this containing shape is designed to organize related classifiers in a diagram. It is symbolized with a large tabbed rectangle shape.
- Interfaces - similar to a class, except that a class can have an instance of its type, and an interface must have at least one class to implement it.
- Enumerations - representations of user-defined data types. An enumeration includes groups of identifiers that represent values of the enumeration.
- Objects - instances of a class or classes. Objects can be added to a class diagram when representing either concrete or prototypical instances.
- Artifacts - model elements that represent the concrete entities in a software system, such as documents, databases, executable files, software components, and so on.
- Interactions - a term for the various relationships and links that can exist in class and object diagrams. Some of the most common interactions include:
- Inheritance - also known as generalization, this is the process of a child or sub-class taking on the functionality of a parent or superclass. It's symbolized by a straight connected line with a closed arrowhead pointing towards the superclass.
- Bidirectional association - the default relationship between two classes; both classes are aware of each other and their relationship with the other. This association is represented by a straight line between two classes.
- Unidirectional association - a slightly less common relationship between two classes; one class is aware of the other and interacts with it. Unidirectional association is modeled with a straight connecting line that points an open arrowhead from the knowing class to the known class.
More UML Class Diagram Examples
- How to draw a Class diagram
- Class diagram for airline reservation system
- Class diagram for ATM system
- Class diagram for banking system
- Class diagram for hospital management system
- Class diagram for hotel management system
- Class diagram for inventory management system
- Class diagram for library management system
- Class diagram for login page
- Class diagram for online shopping system
- Class diagram for railway reservation system
Use Lucidchart to collaborate and create UML diagrams when you start an account for free today! No plugins or download required.