UML Class Diagrams
Unified Modeling Language (UML) class diagrams provide a quick summary of a class and can also be used to denote relationships between classes.
- The most basic UML class diagram consists of one class.
- In each class the following are specified:
- The class name
- Attributes (a.k.a., fields or characteristics) of the class or instance from the class.
- Each attribute shown with its name a colon and its type
- The minus sign in front of each attribute indicates that the attribute is
private
.
- Behaviors (a.k.a., methods or member functions) of the class or instance from the class.
- Each method is shown with its name, parameter list, a colon, and the return type.
- The plus sign in front of each attribute indicates that the method is
public
.
Associations
- Often there are relationships between classes and/or objects.
- One very general relationship is known as an association which links two objects.
- Here we denote the relationship between a
Student
instance and aCity
instance (representing the student's hometown). - The role name (found next to the arrow) corresponds to the attribute name,
hometown
(in theStudent
class). - Since the
hometown
attribute in theStudent
class can hold a reference to aCity
object, it provides a way to navigate from a student to his/her home town. - Here the arrow head on the association indicates a one-way navigation from student to hometown.
- The diagram does not denote a way to navigate from a city to all the students who consider it their home town.
- Some associations may involve objects from the same class.
- For example:
- Here the
buddy
attribute in oneStudent
instance can hold a reference to anotherStudent
instance.
Aggregation
- Some associations imply a "whole-part" relationship.
- Consider the relationship between
Section
objects andStudent
objects:
- The unfilled (open) diamond on the
Section
side of the association line denotes that the students belong to (or make up) the section object. - This kind of association is called an aggregation.
- The 0.. on the
Student
side indicates that there can be zero or more students in a section. - This is called the multiplicity of the participation of the
Student
object(s) in this aggregation. - Aggregation is made up of objects that can be shared or exchanged (they are not "owned" by the aggregating object).
Composition
- Some "whole-part" relationships represent an even stronger link.
- The filled (closed) diamond on the
Course
side of the association line denotes that the sections belong exclusively to a course and cannot exist independently. - This kind of association is called a composition.
- Note that the
sections
attribute does not need to be listed in the list of attributes in theCourse
UML class diagram since it is shown on the composition line.
Dependency
- Often times one class may make use of another class.
- This is known as a dependency since one class is dependent upon another class.
- We represent such a dependency with a dashed line with an arrow. The arrow points to the class that is depended upon.
- For example, if a
BankAccount
class makes use of methods from theMath
class in order to calculate interest, we could represent that as follows:
- Both
pow()
andsqrt()
in theMath
class are underlined to denote that they are class (static
) methods.
Inheritance
- The inheritance relationship is shown in UML class diagrams using an open arrow from the subclass to the superclass.
- The open arrow signifies that the superclass is a generalization of the subclass.
- Here is the UML class diagram for the
Shape
andCircle
classes described on the Inheritance page. - In addition, a
Rectangle
class is shown. - The two fields of the
Rectangle
class are declared asprotected
which is signified with the # symbol.
Abstract Classes/Methods
- The name of an abstract class is italicized.
- In addition, abstract methods are italicized.
- For example,
draw
,erase
, andzoom
are abstract methods in the abstractShape
class below.
Interfaces
- The name of an interface is italicized and «interface» is placed above the interface name.
- A dashed line going from a class to an interface, terminating with an open arrow signifies that the class implements the interface.
- For example, the following diagram indicates that the
LoginScreen
class implements theSerializable
interface.
Example Diagram
- The name of an abstract class appears in italics and may be preceded by «abstract». See
DesktopItem
. - The name of an interface appears in italics and is preceded by «interface». See
Group
. - Abstract methods appear in italics. See
DesktopItem.open()
. - The inheritance relationship is shown using a line with an open arrow pointing from the subclass to the superclass.
- A class implementing an interface is shown using a dotted line with an open arrow pointing from the class to the interface it implements.
- A class may indicate that it makes use of another class/interface using a dotted line with an arrow pointing to the class that is used.
- A class indicates that it is contains object(s) from another class as a field using a line. (See line from
Folder
toDesktopItem
)- At the end of the line next to the class that contains the field should be a open diamond.
- At the end of the line next to the class that is contained, a number, or range of numbers, indicates how many of the objects are contained. In this example, the
Folder
can contain zero or moreDesktopItem
s.