Lesson 2 - Class Diagrams
Lesson 2 - Class Diagrams
Class Diagrams
Diagrams in UML
• A diagram is the graphical presentation of a set of elements, most often
rendered as a connected graph of vertices (things) and arch (relationship)
• UML includes nine (9) diagrams
3
What is a class?
• A Class is a description of a group of objects with common
properties (attributes), common behavior (operations), common
relationships to other objects, and common semantics.
Class Diagrams
• Backbone of nearly all OO Methods.
• A class diagram describes the types of objects in the system
and the various kinds of static relationships that exist among
them.
• It also shows the attributes and services of a class and the
constraints that apply to the way objects are connected.
Book
Accno
Title
UML Notation for a Class Author
GetAuthor(int)
Visualizing a Class
• Represented by a rectangle
• Class naming
– represented by a word with an initial uppercase letter.
– Appears near the top of the rectangle
– If the class has two word name, join the two words together and
capitalize the first letter of the second word.
Attributes
• A property of a class
• Describes a range of values that the property may hold in objects of
that class.
• A class may have zero or more attributes.
• If name consists of more than one word the words are jointed and
each word begins with a uppercase letter.
Operations
• An operation is some action that one object performs upon another
in order to response a reaction.
• A class may have more than one operation or no operations.
• The operation name should always starts with a
simple letter and
• If the operation name consists of more than one word,
then all the words should be combined by capitalizing
the first letter of every word except the first word.
Activity
Visualize any class you know and indicate attributes and operations of
that class
Working with Relationships
The Need for Relationships
• Relationships provide the conduct for object interactions.
• A relationship is a semantic connection between classes.
• It allows one class to know about the attributes, operations of another
class.
• In object oriented analysis and design there are four main types of
relationships.
• Those are generalization, dependency, realization and association.
• Aggregation and Composition are special types of association
relationships.
Dependency
• Class A depends on Class B, if any change to a class B forces a change to
a Class A. i.e. any operation occurred in class B changes the states, or the
values of the attributes of class A.
• In UML, the dependency is rendered using a dashed directed line,
which directed to the things or the class being depended on.
• For example, if Class A depends on B, then it can be represented as in
below figure
Dependency
• For example, consider the Window Class and Event Class
• The double click event on window icon will maximize the window.
• Therefore, operations fired in the Event class changes the status of
Window Class.
• Therefore, we can say, Window class uses the Event class, or Window
class depends on Event class
Association
1 Exactly one
0..* Zero or more
1..* One or more
0..1 Zero or one
5..8 Specific Range (5,6,7 or 8)
Activity 1
Department is managed by exactly one Head of the Department (HOD). And each
HOD manages only one department.
Activity 2
In the recursive association, let assume that one or more employees are supervised
by exactly supervisor. And each employee can have only one supervisor.
Activity 3
A student in an institute follows one or more courses and a particular course is
followed by at least twenty or more students.
Aggregation
• Aggregation is a specialized form of association in which a whole is related
to its part(s).
• This is also known as whole-part relationship or has-a relationship,
meaning that an object of the whole has object of the part.
• Aggregation is typically rendered as an association with an open diamond
(empty) at the whole end.
• Example: Each honours course requires/has six or more module. And a
module may offer to more than one course
1..* 1..*
Aircraft Engine
Composition
• Composition is a special kind of aggregation.
• The whole strongly owns it’s parts.
• If the whole object is copied or deleted, then the parts are copied or
deleted with it.
• Multiplicity at whole end must be 1.
• Composition is rendered by filled diamond at the whole end.
• E.g. A faculty has one or more department. A department belongs to only
one faculty.
1 1..*
School Department
Generalization
• Parent class is a generalization of the child class, i.e. generalization is a
relationship between a parent (known as super‐class) and a child
(known as sub‐class).
• This relationship also known as ‘is a’ or ‘is a kind of’ relationship.
• The relationships such as Teacher is a Person, Rose is a Flower are
examples for generalization.
• A class can have zero, one or more parents.
• In Java, a class is allowed to have only one parent.
• A class which has no parents but has one or more children is known
Base Class.
• And a class with no children is known as a Leaf Class.
Generalization
• In UML, the generalization relationship is rendered as a sold directed
line with a large open arrowhead pointing to the parent class.
• For example, we can render the Teacher is a Person relationship as
shown in below figure.
Inheritance (Generalization)
• An inheritance relationship:
– is not a relationship between different objects.
– is a relationship between different classes.
– Rolenames are not used.
– Multiplicity does not apply.
Abstract Class
• Intended only as bases for inheritance
• Provides no objects of their own
• Indicated by writing the name of the abstract class in italics
e.g.
Stereotypes and Classes
A Stereotype is a mechanism you can use to categorize your classes.
– Say you want to quickly find all of the forms in the model,
– You could create a stereotype called form, and assign all of your
windows this stereotype.
– To find your forms later, you would just need to look for the classes
with that stereotype.
Stereotypes and Classes
• There are three primary class stereotypes in UML.
Stereotypes and Classes
Boundary Class:
Boundary Class:
• At a minimum there must be, one Boundary class for every actor-
use case interaction.
• Boundary class allows actor to interact with the system.
Entity Class
Entity Class
Control Class:
Task:
• Draw a class diagram which consists of all the classes in your system
their attributes and operations, relationships between the classes,
multiplicity specifications, and other model elements that you find
appropriate.
Object Diagrams
Object Diagrams
• Elements from the problem domain for a hockey league. A hockey league is made
up of at least four hockey teams. Each hockey team is composed of six to twelve
players, and one player captains the team. A team has a name and a record.
Players have a number and a position. Hockey teams play games against each
other. Each game has a score and a location. Teams are sometimes lead by a
coach. A coach has a level of accreditation and a number of years of experience,
and can coach multiple teams. Coaches and players are people, and people have
names and addresses. Draw a class diagram for this information, and be sure to
label all associations with appropriate multiplicities.