Lecture 07 - Class Diagram
Object Oriented Analysis and Design
Muhammad Zeeshan Sabir
Classes
A class is simply represented as a box with the name of the
class inside
◦ The diagram may also show the attributes and
operations
◦ The complete signature of an operation is:
operationName(parameterName: parameterType …): returnType
2
Objects
An instance of a class.
An object represents an entity in the real world such as a person, thing or concept etc. An object
is identified by its name.
An object consists of the following two things:
1. Properties: Characteristics of an object
2. Functions: Actions that can be performed by an object.
Properties of Object
The characteristics of an objects are known as its properties or attributes.
Each object has its own properties.
These properties can be used to describe the object. E.g.
◦ The properties of an object person can be as follows:
◦ Name
◦ Age
◦ Weight
◦ The properties of an object car can be as follows:
◦ Color
◦ Price
◦ Model
Functions of Object
An object can perform different tasks and actions. The actions that can be performed by an
object are known as functions or methods.
◦ Start Car
◦ Stop Start
◦ Reverse Stop
Accelerate
Reverse
The set of all functions of an object represents the behavior of the object. It means that the
overall behavior of an object can be determined by the list of functions of that object.
Class
A class is a construction plan for a set of similar objects that appear in system.
Classes have functions and attributes.
◦ Attributes includes all fields of objects.
◦ Function may omit get/set method.
It is represented by rectangular box with the name of class.
Class attributes
attributes (fields, instance variables)
visibility name : type [count] = default_value
visibility:
◦ + public
◦ # protected
◦ - private
◦ / derived
underline static attributes
derived attribute: not stored, but can be computed from other attribute values
Class Functions/methods
Operations / methods
Visibility name (parameters) : return_type
visibility:
◦ + public
◦ # protected
◦ - private
◦ ~ package (default)
underline static methods
parameter types listed as (name: type)
omit return_type on constructors and when return type is void
method example:
◦ + distance(p1: Point, p2: Point): double
Visibility of Attributes and
Operations
Visibility Symbol Accessible To
Public + All objects within your system.
Protected # Instances of the implementing class and its
subclasses.
Private - Instances of the implementing class.
Example of a Class Diagram
Video Rental System
visibility multiplicity class name
Customer 1..* 1..*
Video
-CID: int -cassetteID : int
-name: String rents -cassetteVolumeNo: int
+rentMovie()
+authenticateCustomer ()
attributes relationship methods
Relationship between classes
Represents a connection between multiple classes or a class and itself
basic categories:
◦ Association relationships
◦ Generalization relationships
◦ Composition relationships
◦ Aggregation relationships
Association Relationship
A bidirectional semantic connection between classes
Type:
◦ name of relationship
◦ role that classes play in the relationship
Labelling associations
◦ Each association can be labelled, to make explicit the
nature of the association
13
Analyzing and validating
associations
◦ Many-to-one
◦ A company has many employees,
◦ An employee can only work for one company.
◦ This company will not store data about the moonlighting activities of employees!
◦ A company can have zero employees
◦ E.g. a ‘shell’ company
◦ It is not possible to be an employee unless you work for a company
worksFor 1
Employee * Company
14
Analyzing and validating
associations
◦ Many-to-many
◦ An assistant can work for many managers
◦ A manager can have many assistants
◦ Assistants can work in pools
◦ Managers can have a group of assistants
◦ Some managers might have zero assistants.
Assistant * 1..** Manager
supervisor
15
Analyzing and validating
associations
◦ One-to-one
◦ For each company, there is exactly one board of directors
◦ A board is the board of only one company
◦ A company must always have a board
◦ A board must always be of some company
1 1
Open in Umple
Muhammad Zeeshan Sabir LECTURE 3: INTRODUCTION TO UML 16
Association Relationship
Role type shown by:
◦ drawing line between classes
◦ Indicating with a plus sign before the role name
Patient
+ primary insurance
carrier
Association Class
Attributes or operations can be assigned to the relationship between one or more
classes rather than to a class itself.
An association class is represented by a class and an association that are
connected by a dashed line.
The association can be binary or n-ary.
Association classes
◦ Sometimes, an attribute that concerns two associated classes cannot
be placed in either of the classes
◦ The following are equivalent
19
Cont…
Binary Association
Multiplicity:
◦ Documents how many instances of a class can be associated with one instance of
another class
◦ multiplicity (how many are used)
◦ * ⇒0, 1, or more
◦ 1 ⇒1 exactly
◦ 2..4 ⇒between 2 and 4, inclusive
◦ 3..* ⇒3 or more (also written as “3..”)
◦ name (what relationship the objects have)
◦ navigability (direction)
Generalization
Is-a relationship.
hierarchies drawn top-down
arrows point upward to parent
line/arrow styles indicate whether parent is
◦ class:
◦ solid line, black arrow
◦ abstract class:
◦ solid line, white arrow
◦ interface:
◦ dashed line, white arrow
often omit trivial / obvious generalization
Generalization
Specializing a superclass into two or more subclasses
◦ A generalization set is a labeled group of generalizations with a
common superclass
◦ The label (sometimes called the discriminator) describes the criteria
used in the specialization
22
Avoiding unnecessary
generalizations
Inappropriate hierarchy of classes, which should be instances
23
Avoiding unnecessary
generalizations (cont)
Improved class diagram, with its corresponding instance diagram
24
Handling multiple discriminators
◦ Creating higher-level generalization
25
Handling multiple discriminators
26
Generalization Relationship
Enables the analyst to create classes that inherit attributes and operations of other classes
Represented by a-kind-of relationship
Person
Employee Customer
Manager Engineer
Generalization Relationship
Employee
hireDate
receivePay
performWork
Manager Engineer
department certifications
bonus
hireEmployee analyze
promoteEmployee design
Aggregation
Aggregation is a special form of association.
◦ Shared Aggregation
◦ Composition
Aggregation
Part relationship
Independent existence.
Composition
Symbolized by black diamond/ Denoted by placing a diamond nearest class
representing the Composition
The part live and die relationship.
A special type where parts are destroyed when the whole is destroyed.
Objects of Class2 live and die with Class1.
Class2 cannot stand by itself
1 provides 0..1 Medical History
Patient
n-array Aggregation
Relationship between more than two classes.
It is represented by hallow diamond.
Aggregation hierarchy
33
Composition
◦ A composition is a strong kind of aggregation
◦ if the aggregate is destroyed, then the parts are destroyed as well
◦ Two alternatives for addresses
34
Example: Class Diagram
Example: Class Diagram
Case Study
A university consists of multiple faculties which are composed of university various institutes.
Each faculty and each institute has a name. An address is known for each institute.
Each faculty is led by a dean, who is an employee of the university.
The total number of employees is known. Employees have a social security number, a name, and
an e-mail address. There is a distinction between research and administrative personnel.
Research associates are assigned to at least one institute. The field of study of each research
associate is known. Furthermore, research associates can be involved in projects for a certain
number of hours, and the name, starting date, and end date of the projects are known.
Some research associates teach courses. They are called lecturers.
Courses have a unique number (ID), a name, and a weekly duration in hours.