0% found this document useful (0 votes)
76 views74 pages

Session 7

The document discusses UML class diagrams, including their basic components and how they are used to model object-oriented systems. It describes the key elements of a class diagram, including classes, attributes, operations, and relationships. It provides examples of class diagrams and explains how they visually represent classes, objects, and the connections between them. The document emphasizes that class diagrams are commonly used for software documentation and modeling because they communicate system structure clearly and concisely through graphical notation.

Uploaded by

yadvji568
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views74 pages

Session 7

The document discusses UML class diagrams, including their basic components and how they are used to model object-oriented systems. It describes the key elements of a class diagram, including classes, attributes, operations, and relationships. It provides examples of class diagrams and explains how they visually represent classes, objects, and the connections between them. The document emphasizes that class diagrams are commonly used for software documentation and modeling because they communicate system structure clearly and concisely through graphical notation.

Uploaded by

yadvji568
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

18CSC202J - OBJECT ORIENTED DESIGN

AND PROGRAMMING

Session 7

Topic : UML Class Diagram, its components,


Class Diagram relations and Multiplicity
UML Diagrams - Introduction

•UML stands for Unified Modeling Language


•It is a modern approach to modeling and documenting software
•It is one of the most popular business process modeling techniques
•It is based on diagrammatic representations of software components
•An old proverb says, “A picture is worth a thousand words”
•By using visual representations, we are able to better understand possible
flaws or errors in software or business processes

8/24/2022 2
What is UML and Why we use UML?
▪ More description about UML:
▪ It is a industry-standard graphical language for specifying, visualizing,
constructing, and documenting the artifacts of software systems

▪ The UML uses mostly graphical notations to express the OO analysis and
design of software projects.

▪ Simplifies the complex process of software design


What is UML and Why we use UML?
▪ Why we use UML?
▪ Use graphical notation: more clearly than natural language (imprecise) and
code (too detailed).

▪ Help acquire an overall view of a system.

▪ UML is not dependent on any one language or technology.

▪ UML moves us from fragmentation to standardization.


• UML diagrams are being made with the intent of showcasing a visual
representation of any system and are supported by actors, actions and
artifacts.

• When any business analyst utilizes UML diagrams, it makes his/her analysis
tasks much easier and leads to very transparent communication with the
developers.

• UML diagram serves as a complete design that requires the actual


implementation of the system or software.

• Often, this is done by using CASE tools (Computer Aided Software


Engineering Tools)

• It needs more expertise and user training


Overview of UML Diagrams
Behavioral
: behavioral features of a system / business
Structural process
: element of spec. irrespective of time
• Activity
• State machine
• Class
• Use case
• Component
• Interaction
• Deployment
• Object
• Composite structure Interaction
• Package : emphasize object interaction

• Communication(collaboration)
• Sequence
• Interaction overview
• Timing
UML Diagram Examples
USE CASE DIAGRAM
CLASS DIAGRAM
STATE DIAGRAM
ACTIVITY DIAGRAM
SEQUENCE DIAGRAM
COLLABORATION DIAGRAM
COMPONENT DIAGRAM
DEPLOYMENT DIAGRAM
UML Class Diagram
UML Class diagram :
• The UML Class diagram is a graphical notation used to construct and
visualize object oriented systems.
• A class diagram describes the structure of a system such as Classes and
their attributes , operations (or methods) and the relationships among
objects.
• A class diagram is used to show the existence of classes and their
relationships in the logical view of a system.
Class Diagram

•It is the most common diagram type for software documentation


•Class diagrams contain classes with their attributes (data members) and their
behaviours (member functions)
ClassName
•Each class has 3 fields:
Attributes
•Class Name
•Attributes Behaviours
•Behaviours
•The relation between different classes makes up
a class diagram

8/24/2022 19
Basic components of a class diagram
The standard class diagram is composed of three sections:
Upper section: Contains the name of the class. This section is always
required, to know whether it represents the classifier or an object.

Middle section: Contains the attributes of the class. Use this section to
describe the qualities of the class. This is only required when describing a
specific instance of a class.

Bottom section: Includes class operations (methods). Displayed in list


format, each operation takes up its own line. The operations describe how a
class interacts with data.
Classes
A class is a description of a set of objects
ClassName that share the same attributes,

attributes operations, relationships, and


semantics.
operations

Graphically, a class is rendered as a


rectangle, usually including its name,
attributes, and operations in separate,
designated compartments.
Class Names

The name of the class is the only required


ClassName
tag in the graphical representation of a class.
attributes It always appears in the top-most
compartment.
operations
Class Attributes

An attribute is a named property of a


Person
class that describes the object being
name : String modeled.
address : Address
birthdate : Date In the class diagram, attributes appear
ssn : Id in the second compartment just below
the name-compartment.
Class Attributes (Cont’d)
Attributes are usually listed in the form:

Person
attributeName : Type

name : String
address : Address A derived attribute is one that can be computed
birthdate : Date from other attributes, but doesn’t actually exist.
/ age : Date
For example,
ssn : Id
a Person’s age can be computed from his birth
date.
A derived attribute is designated by a
preceding ‘/’ as in:
/ age : Date
Class Attributes (Cont’d)

Person
Attributes can be:
+ name : String + public
# address :
Address # protected
# birthdate : Date - private
/ age : Date
- ssn : Id / derived
Class Operations

Person
name : String
address :
Address
birthdate : Date
ssn : Id
eat
sleep Operations describe the class behavior
work
and appear in the third compartment.
play
Class Operations (Cont’d)

PhoneBook

newEntry (n : Name, a : Address, p : PhoneNumber, d :


Description)
getPhone ( n : Name, a : Address) : PhoneNumber

You can specify an operation by stating its signature: listing the


name, type, and default value of all parameters, and, in the case of
functions, a return type.
Depicting Classes
When drawing a class, you needn’t show attributes and operation
in every diagram.

Person Person Person


name : String
birthdate : Date
Person ssn : Id
name Person eat()
address sleep()
birthdate eat work()
play play()
Class Responsibilities
A class may also include its responsibilities in a class diagram.

A responsibility is a contract or obligation of a class to perform


a particular service.

SmokeAlarm

Responsibilities

-- sound alert and notify guard station


when smoke is detected.

-- indicate battery state


RULES TO BE FOLLOWED:

•Class name must be unique to its enclosing namespace.


•The class name begins in uppercase and the space between multiple words is
omitted.
•The first letter of the attribute and operation names is lowercase with
subsequent words starting in uppercase and spaces are omitted.
• Since the class is the namespace for its attributes and operations, an
attribute name must be unambiguous in the context of the class.
Class Diagram
•Basic Components of a Class Diagram
•Upper Section
It contains the name of the class
•Middle Section
It contains the attributes of the class
•Bottom Section
It includes methods
•Let us model a Zoo System
•What’s in a Zoo? Animals
•Let us name the class Animal
•If our class is Animal, an instance of that class would
be a specific animal

8/24/2022 31
Class Diagram – cont..
Attributes : Second Section
• How would you identify each instance of
that class, i.e., a specific animal?
• Using Attributes
• What are Attributes?
• A significant piece of data containing values that describe each instance of that class
• They are also known as fields, variables or properties
• Format for specifying an attribute:
visibility attributeName : Type [multiplicity] = DefaultValue {property string}
• Visibility: It sets the accessibility of an attribute / method
• Private (-) Visible only to the elements within the same class
• Public (+) Visible to any element that can see the class
• Protected (#) Visible to the elements in the class and its sub class
• Package (~) Visible to elements within the same package
8/24/2022 32
Class Diagram – cont..
• Methods : Third section
• They are also known as operations or functions
• It specifies the behaviour of a class
• Eg: We may want to change the name of an Animal
• We could add a method setName
• We could also create a method for eating, since all of
our animals eat
• Lets take another example : Employee Details

8/24/2022 33
Multiplicity and Relationships

8/24/2022 34
Relationships
Class Relationships
The essential connections among classes include
•Association
•Generalization
•Aggregation
•Composition
Association
• An association is a relationship between two separate
classes. It joins two entirely separate entities.
• There are four different types of association:
– Bi-directional
– Uni-directional
– Aggregation (includes composition aggregation)
– Reflexive.
• Bi-directional and uni-directional associations are the
most common ones.
• This can be specified using multiplicity (one to one, one
to many, many to many, etc.).
• A typical implementation in Java is through the use of
an instance field. The relationship can be bi-directional
with each class holding a reference to the other.
Association Relationships
If two classes in a model need to communicate with each other,
there must be link between them.

An association denotes that link.

Student Instructor
Association Relationships (Cont’d)
We can indicate the multiplicity of an association by adding
multiplicity adornments to the line denoting the association.

The example indicates that a Student has one or more


Instructors:

Student Instructor
1..*
Association Relationships (Cont’d)

The example indicates that every Instructor has one or more


Students:

Student Instructor
1..*
Association Relationships (Cont’d)
We can also indicate the behavior of an object in an association
(i.e., the role of an object) using rolenames.

teaches learns from


Student Instructor
1..* 1..*
Association Relationships (Cont’d)
We can also name the association.

membership
Student Team
1..* 1..*
Association Relationships (Cont’d)
We can specify dual associations.

member of
1..* 1..*
Student Team
1 president of 1..*
Association Relationships (Cont’d)
We can constrain the association relationship by defining the
navigability of the association. Here, a Router object requests
services from a DNS object by sending messages to (invoking
the operations of) the server. The direction of the association
indicates that the server has no knowledge of the Router.

Router DomainNameServer
Association Relationships (Cont’d)
Associations can also be objects themselves, called link classes
or an association classes.

Registration
modelNumber
serialNumber
warrentyCode

Product Warranty
Association Relationships (Cont’d)

A class can have a self association.

next

LinkedListNode
previous
Inheritance
• Indicates that child (subclass) is considered to be a
specialized form of the parent (super class).
• For example consider the following:
Example

• Above we have an animal parent class with all


public member fields.
• You can see the arrows originating from the duck,
fish, and zebra child classes which indicate they
inherit all the members from the animal class.
• Not only that, but they also implement their own
unique member fields.
• You can see that the duck class has a swim()
method as well as a quack() method.
Interface
A relationship between two model elements, in which one
model element implements/executes the behavior that the
other model element specifies.
Dependency Relationships
A dependency indicates a semantic relationship between two or
more elements.

The dependency from CourseSchedule to Course exists because


Course is used in both the add and remove operations of
CourseSchedule.

CourseSchedule
Course
add(c : Course)
remove(c :
Course)
Dependency - Aggregation
• A special form of association which is a
unidirectional (a.k.a one way) relationship between
classes.
• The best way to understand this relationship is to
call it a “has a” or “is part of” relationship.
• For example, consider the two classes: Wallet and
Money.
• A wallet “has” money. But money doesn’t
necessarily need to have a wallet so it’s a one
directional relationship.
Association Relationships (Cont’d)
We can model objects that contain other objects by way of
special associations called aggregations and compositions.

An aggregation specifies a whole-part relationship between an


aggregate (a whole) and a constituent part, where the part can
exist independently from the aggregate. Aggregations are
denoted by a hollow-diamond adornment on the association.
Aggregation ( (open diamond)
• It is a special type of association that specifies a whole and its parts, where a part can exist
outside the whose
• Lets take a group of tortoises, called creep

• Any of our zoo’s tortoises could be part of a


creep

ASSOCIATION
A tortoise could leave the creep at any point and still
Exist on its own

8/24/2022 54
An aggregation specifies a whole-part relationship between an aggregate (a whole) and
a constituent part, where the part can exist independently from the aggregate.
Aggregations are denoted by a hollow-diamond adornment on the association.

Engine
Car
Transmission
Generalization Relationships
A generalization connects a subclass
Person to its superclass.

It denotes an inheritance of attributes


and behavior from the superclass to

Student the subclass and indicates a


specialization in the subclass of the
more general superclass.
Generalization Relationships (Cont’d)
UML permits a class to inherit from multiple superclasses, although some
programming languages (e.g., Java) do not permit multiple inheritance.

Student Employee

TeachingAssistant
Generalization Relationships (Cont’d)
Multilevel Inheritance
Relationships b/w Classes
Inheritance (Generalization)
• Lets say in our zoo, the only animals we have are tortoises, otters and Slow Loris
• We make 3 new classes: Tortoise, Otter and
Slow Loris
• These subclasses inherit all the attributes and
methods of the superclass
• We could add an attribute specific to Otter
in the Otter class
• The main advantage of inheritance is that
if we wanted to change or add an attribute
for all animals, we need not make change in
every sub class
• We just make the change to the base class ‘Animal’ class and it applies across all sub classes
8/24/2022 59
Relationships b/w Classes
Abstraction
• In this example ‘Animal’ is the Abstract Class
• We will create instance for only the subclasses
• We wouldn’t instantiate the Animal class itself
• The Animal class is just a way to simplify things and keep the code “dry”
• To show that it is an Abstract class, we can write the name of the class in italics Animal or we
can enclose it like <<Animal>>

8/24/2022 60
Relationships b/w Classes
Association
• If we had a class for Sea Urchin
we could draw an association
• Sea otter is a marine mammal in
the north Pacific Ocean
• They eat mostly hard-shelled invertebrates
including sea urchins.

ASSOCIATI
ON

8/24/2022 61
Composition
• It is a special type of association that specifies a whole and its parts, where the part cannot
exist outside the whole
• Example: Let us say we have several different visitors centers in our zoo. And each of those
visitor centers has a lobby and a bathroom
• Now if one of our visitors centers was torn down, the lobby and the bathroom of that visitor
center are torn down as well.
• Composition : A child object cannot exist without its parent object

8/24/2022 62
Association Relationships (Cont’d)
A composition indicates a strong ownership and coincident
lifetime of parts by the whole (i.e., they live and die as a
whole). Compositions are denoted by a filled-diamond
adornment on the association.
A composition indicates a strong ownership and coincident
lifetime of parts by the whole (i.e., they live and die as a
whole). Compositions are denoted by a filled-diamond
adornment on the association.

Scrollbar
1 1

Window Titlebar
1 1

Menu
1 1 .. *
Composition
• A restricted form of Aggregation in which two entities (or you can
say classes) are highly dependent on each other.
• A human needs a heart to live and a heart needs a human body to
function on. In other words when the classes (entities) are
dependent on each other and their life span are same (if one dies
then another one does too) then it’s a composition.
Multiplicity
• After specifying the type of association relationship by connecting the
classes, you can also declare the cardinality between the associated
entities. For example:

• The above UML diagram shows that a house has exactly one kitchen,
exactly one bath, at least one bedroom (can have many), exactly one
mailbox, and at most one mortgage (zero or one).
Multiplicity Constraints
• A multiplicity constraint can be
– a single number
– a “*”, meaning an arbitrarily large number or
simply “many”
– a range, denoted by “min..max”
– a sequence of single numbers and ranges

Coming up: Dependencies


Multiplicity
• It allows us to set numerical constraints on relationships
• Some types of multiplicity are
• 1 – Just one
• n – Specific number
• 0..* - Zero to many
• 1..* - One to many (At least one)
• 0..1 – Zero or one (Optional)
• m..n – Specific number range

8/24/2022 69
Multiplicity Constraints
is registered for>
Student Semester
1..*
0..* 1
tak

g>
es>

rin
du
ld
he
0..8
1..*

is
teaches> Class
Instructor
Section
1..3 0..6
1..* is
in
st
a
<works for

nc
e
of
>
1 sponsors>
1 1..*
Department Course
Coming up: Multiplicity Constraints
Questions
• From the previous diagram
– How many classes can a student take?
– Do you have to be registered in any classes to be a
student?
– Do I need to teach this class to be an Instructor?
Do I need to teach ANY classes?
1. What type of core-relationship is represented by the symbol in the figure below?

a) Aggregation
b) Dependency
c) Generalization
d) Association
Answer: a
2. Which core element of UML is being shown in the figure?

a) Node
b) Interface
c) Class
d) Component
Answer: d

8/24/2022 72
3. What type of relationship is represented by Shape class and Square ?

a) Realization
b) Generalization
c) Aggregation
d) Dependency
Answer: b

8/24/2022 73
4. Consider a directed line (->) from the relation ship set advisor to both entity sets
instructor and student. This indicates ___________ cardinality.

a. One to many
b. One to one
c. Many to many
d. Many to one
Answer: b

5. An entity set that does not have sufficient attributes to form a primary key is
termed as a ____
a. Strong entity set
b. Variant set
c. Weak entity set
d. Variable set
Answer: c

8/24/2022 74

You might also like