University of Computer Studies, Yangon
Chapter 8
Class and Object Diagram
Part I
Faculty of Information Science
Introduction to Class and Object
What kind of Model?
Static Model
What is object diagram?
An object diagram provides a ‘snapshot’ of a system,
showing the objects that actually exist at a given moment and
the links between them.
Valid
Invalid
What is Class Diagram
A class diagram acts as a kind of specification for a system and,
among other things, states what kinds of objects can exist, what
data they encapsulate and how objects in the system can be
related to each other.
DATA TYPES
UML defines a number of primitive data types and also
provides a mechanism whereby new types can be defined.
Data types represent simple, unstructured kinds of data such as
numeric, character and Boolean values.
Instances of data types are called data values.
Data values differ from objects in that they have no notion of
identity.
Data Type in UML
The data types available in UML fall into three categories.
First, there are a number of predefined types :
Integer
String
Boolean
The UML notation defining these types is shown in
following figure.
<<enumeration>>
Integer String
Boolean
User Defined Enumeration in UML
<<enumeration>>
Color
Red
Green
Blue
Multiplicity
There are a number of places in UML where it is necessary to
state how many times a given entity can occur in some
context.
These are represented by multiplicities.
A multiplicity is a set of integer values.
Multiplicities are defined as a data type in UML and are
represented using multiplicity ranges.
A range consists of a pair of integers separated by dots such
as 0..9.
‘∗’, meaning ‘unlimited’
‘0..100’ therefore represents all the whole numbers from ‘0..100’
inclusive
‘0..∗’ represents all the non-negative integers
‘1..1’, is represented by the single number ‘1’
Classes
What is class diagram?
Class diagrams show the general properties that a
system’s data must satisfy at all times. Given a class
diagram, we can decide whether or not an object diagram
represents a possible state of the system being specified.
What is Class?
• Classes describe the different kinds of object in a
system. As well as a name, classes have attributes and
operations that describe the state and behaviors of the
objects which are instances of the class, namely those
objects described by it
Class in UML
Student Name
name: String
Three Part age :Integer Attributes
grade : String
getName():String
getAge() :String Operations
viewGrade(avgmark:String)
Classes and its Instances
The individual objects, whose structure is described by means of a class,
are known as the instances of the class.
To emphasize the relationship between class and instances, UML
represents objects with the same kind of icon as classes.
The class name is preceded by a colon and underlined and, in addition,
instances may be named.
The dashed line is known as a dependency, and is the notation used by
UML to denote a unspecified sort of relationship between two model
elements.
The label, or stereotype, attached to the dependency gives an indication
of what particular relationship is being depicted.
A class and its Instances in UML
dependency
Jack:Student
e o f>>
ns tanc
<< i
Student
Object 1
<<i
n stan
ce o
f>>
Jill:Student
Class
stereotype
Object 2
Class Multiplicity
The concept of multiplicity can be applied to classes:
the multiplicity of a class specifies the number of instances of
the class that can exist at any one time.
The default multiplicity for a class is ‘zero or more’, implying
that there is no restriction on the number of instances that can be
created.
This default multiplicity does not need to be
shown on a diagram.
The classes can contain a multiplicity annotation, in the top right-
hand corner of the class icon.
The most common use for this notation is to specify that a class is
a singleton class, which can only have one instance.
1
University
Describing Object with Classes
What is attributes?
An attribute is a description of a data field that is maintained by
each instance of a class.
Attributes must be named. In addition to the name, other pieces
of information can be supplied, such as the type of the data
described by the attribute or a default initial value for the
attribute.
Attributes are shown in a separate compartment in the class icon,
beneath the name.
If the attribute type is shown, it is separated from the name by a
colon and initial values follow an equals sign.
Module Module
code
<<instance of>>
code=CS405
title :String title =“UML”
Credits :Integer=15 credits =15
An attribute with class scope
Instance scope, meaning that each instance of the class
can store a distinct value for the attribute.
Some data describes not individual instances but the
collection of all current instances.
Module
mcount :Integer
code
title :String
grade :String=15
Attribute Multiplicity
Module
code
title :String
Exam[0..1]: Date
Grade [*] :String=15
Operation
A module class with attributes and operations
An Operation with class scope
Identifying Objects
Distinct Object with equal State
A class with an identifying Attributes
Visibility of Features
Each feature in a class can have a visibility associated with it, which
specifies the extent to which the feature is available to other classes.
UML defines four levels of visibility:
Public denoted by “+”
Protected denoted by “#”
Private denoted by “-”
Package denoted by “~”
Exercises
Draw UML class and object icons representing each of the following.
Where appropriate, use enumerations and programming language types
to specify attribute types.
(a) A class representing positions, with attributes giving the x and y
co-ordinates of a point. Also show two instances of this class, the origin,
with co-ordinates (0, 0), and the point (100, -40).
point : Position
x=100
<<instance of>> y=-40
Position
+x: Integer
+y: Integer
<<instance of>> origin : Position
x=0
y=0
Exercises
A class representing a counter with operations to set or reset the counter to zero, to
increment and decrement the value stored by a specified amount and to return the
current value.
Counter
+num: Iteger
c1 : Counter
+resettoZero()
+Increment(n:Integer) <<instance of>> vaule=10
+Decrement(n:Integer)
+getValue(): Integer
Exercises
A class representing traffic lights, with attributes to record which colors are
currently illuminated.
Traffic Light
: Traffic Light
+illuminate: Color
<<instance of>> illuminate=red
+getColor(): Color
<<enumeration>>
Color
+Red
+Green
+Blue
Exercise
A class representing a switch that can be turned on or off
Switch
: Switch
+state: Status
state:On
+getState(): Status <<instance of>>
<<enumeration>>
Status
+On
+Off
Thank You
For
Your Attention