Skip Navigation Links > Modeling > UML > Class

Class diagram


A class diagram gives an overview of the structure of a system by showing its classes and the relationships among them. Class diagrams are static - they display what interacts but not what happens when they interact.

The diagram below models a customer order from a retail catalog. The central class is the Order. Associated with the Order is the Customer making the purchase and the Payment. A Payment is one of three kinds: Cash, Check, or Credit. The order contains OrderDetails (line items), each with its associated Item.


Class diagram

UML class notation is a rectangle divided into three parts: class name, attributes, and operations. Names of abstract classes, such as Payment, are in italics. Relationships between classes are the connecting links.

Our class diagram has three kinds of relationships.

An association has two ends. An end may have a role name to clarify the nature of the association. For example, an OrderDetail is a line item of each Order.

A navigability arrow on an association shows which direction the association can be traversed or queried. An OrderDetail can be queried about its Item, but Item cannot query OrderDetail. The arrow also lets you know who "owns" the association's implementation; in this case, OrderDetail has an Item. Associations with no navigability arrows are bi-directional.

The multiplicity of an association end is the number of possible instances of the class associated with a single instance of the other end. Multiplicities are single numbers or ranges of numbers. In our example, there can be only one Customer for each Order, but a Customer can have any number of Orders.

This table gives the most common multiplicities.

Multiplicities

Meaning

0..1

Zero or One instance. The notation n .. m indicates n to m instances.

0..*  or  *

No limit on the number of instances (including none).

1

exactly one instance

1..*

at least one instance

A class diagram can show even more information such as:

Associations in which an object is part of a whole are called aggregations. Composition signifies a strong association in which the part can belong to only one whole - the part cannot exist without the whole. Composition is denoted by a filled diamond at the whole end.

This diagram shows that a BoxOffice belongs to exactly one MovieTheater. Or in other words the MovieTheater "owns a" BoxOffice. Destroy the MovieTheater and the BoxOffice goes away! The collection of Movies is not so closely bound to the MovieTheater. In this case the MovieTheater "has a" Movie.


Composition and Aggregation

The class notation is a 3-piece rectangle with the class name, attributes, and operations. Attributes and operations can be labeled according to access and scope. Here is a new expanded Order class.


Visibility and Scope

The illustration uses the following UML conventions.


Symbol

Access

+

public

-

private

#

protected


A dependency is a relation between two classes in which a change in one may force changes in the other. Dependencies are drawn as dotted lines. In the diagram below, the Co_op class depends on the Company to which a student has been assigned on-the-job training. If you decide to modify Company, you may have to change the Co_op class as well.


Constraints and dependencies

A constraint is a condition that every implementation of the design must satisfy. Constraints can be placed on any kind of model element. They are written in curly braces { }.

Our diagram has two constraints. The one near the top says that a Section can be part of a CourseSchedule only if it is not canceled. The {xor} constraint says that a Student must have a CourseSchedule or a Co_op position but not both.

An interface is a set of operation signatures. In C++, interfaces are implemented as abstract classes with only pure virtual members. In Java and Ada, they're implemented directly.

The class diagram below is a model of a professional conference. The classes of interest to the conference are SessionTalk, which is a single presentation, and Session, which is a one-day collection of related SessionTalks. The ShuttleSchedule with its list of ShuttleStops is important to the attendees staying at remote hotels. The diagram has one constraint, that the ShuttleStops are ordered.


Class diagram with interfaces

There are three interfaces in the diagram: IDated, ILocatable, and ITimed. The names of interfaces typically begin with the letter I. A class, such as ShuttleStop, with operations matching those in an interface, such as ILocatable, is a realization/implementation of the interface.

An interface is a kind of stereotype. Stereotypes, which provide a way of extending UML, are new kinds of model elements created from existing kinds. A stereotype element contains the stereotype name in guillemets (angle-brace pairs).

There are two acceptable notations for interfaces in the UML. The first is illustrated above. The second uses the lollipop or pushpin notation.


Lollipop interface notation

In lollipop notation, the interfaces are circles with lines connected to the realizing classes. Since it is more compact but leaves out some detail, the lollipop notation simplifies the original diagram.


Self test

Top-of-page | Use-Case | Class | Package | Sequence | Communication | State | Activity | Deployment