UML Class Diagram Summary
UML Class Diagram Summary
This document provides a summary of the main features of a class diagram. Please
consult a UML textbook for further details.
Class
Structure
Each class can have a name, a list of attributes and a list of operations, as shown
below. Each row represents a compartment.
The top compartment contains the class name, e.g. Car, and an optional stereotype.
A stereotype describes an additional characteristic of the UML item. The stereotype
is a word enclosed in « and » characters (called guillemets) or if those characters
aren’t available, simply << and >>. Example stereotypes that you have seen are
<<interface>> and <<enumeration>>. You may also see another property in the top
compartment that is used to indicate additional information; an example later in this
document uses {abstract}.
The middle compartment contains the attributes (instance variables) for the class.
Each attribute has an accessibility operator, a name and a type. Note, the UML way
to write attributes is different to how you would write the same thing in a particular
programming language. For example, in UML, you could say that there is a private
attribute called name that is of type String:
- name : String
In Java, this would be written as:
private String name;
The bottom compartment contains a list of operations for the class. Each operation
has an accessibility operator, a name, a list of parameters and an optional return
type. Each parameter has a type.
Sometimes, a simpler view of the class might be drawn to show that other classes in
the diagram use the class, but not provide all of the details in the particular diagram.
This will contain the top compartment and the other two compartments will be empty.
Car
You might also see examples where only the top compartment is shown.
Car
Pet
{abstract}
+ getSound()
+ otherMethod(one: String)
+ anotherMethod() : String {abstract}
Static
Methods
Some designs make use of static methods – Java calls these class methods. These
are methods that can be called without needing to create an instance first. The main
method that starts a Java program is static. There are other times when a method
might be static, but most methods/fields will not be static.
To show that a method is static, you underline the method signature. For example:
PetShop
+ PetShop()
+ main(args: String[])
+ openShop()
Associations
An association shows a link between classes; the main association type is a
has-a/has-some relationship. The first type of association indicates that one class
holds a reference to another class. The reference is held as an attribute in the
original class.
This type of association uses a solid line to connect the two classes. Each end can
include multiplicities, explained below, and the role that the class plays in the
association.
Car Person
- numDoors: int
0..* 1..1 - name: String
- license: String - cars + owner
In the above example, the Person class includes an attribute called car that refers to
the Car object that is referenced from the Person object. Also, the Car class includes
an attribute owner that refers to a Person.
Multiplicities
Common multiplicities are:
1 (or 1..1) – There is a one to one relationship. For example, each Car has only
one owner.
0..1 – There can be 0 or 1 item.
1..* - There can be a reference to one or more items.
* (or 0..*) - There can be a reference to many items or no item is referenced. For
example, each Person can have zero or more cars.
Navigability
Sometimes you would indicate the navigability on one side of the association by
adding an arrow. If you don’t include any navigability then it is assumed that you can
navigate in either direction.
In the following example, a Person object can include references to multiple Car
objects, but the car does not include any references to the same Person object.
Car Person
- numDoors: int
0..* - name: String
- license: String + cars
Dependency
A dependency states that somewhere in Class A, there is a reference to Class B.
This differs from the main type of association in that the reference is either a local
variable or an argument or return type to an operation.
Class B Class A
Inheritance
Inheritance represents the is-a relationship, for example, a Student is a special kind
of Person.
Person Student
The same type of diagram is used to show that one class extends an abstract class.
For example, a Cat is a special kind of Animal, where an Animal is an abstract class.
Animal Cat
{abstract}
Interfaces
–
Implementation
There are two ways that we can represent the situation where a class implements an
interface. The first is to use a dashed line with a closed arrowhead at the end that
represents the interface.
The following example relates to the Animal interface that we used in the lectures.
The Cat implements the Animal interface.
@Override
public String sound(){
return "miaow";
}
}
Enumerations
(enum)
In Java, an enumeration is a way to have a type that has a defined number of
possible values. In the lectures, we had a country code, which we said could be
represented as an enum. This had the possible values UK, US, ES, FR.
In UML, we would write it as a class with two compartments. We would add the
stereotype <<enumeration>> to the name; you might also see this as <<enum>>.
The attributes compartment would list the possible values.
<<enumeration>>
CountryCode
UK
US
ES
FR
Document
History
Version Date Author Description
1.0 2011-02-22 Neil Taylor, Original version, based on original work
Lynda Thomas by Lynda Thomas.
1.1 2012-03-24 Neil Taylor Updated to revise information about
abstract classes. Added information
about static methods.
1.2 2012-03-30 Neil Taylor Corrected information about abstract
classes.