0% found this document useful (0 votes)
44 views56 pages

Chapter3 Class Diagram

Uploaded by

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

Chapter3 Class Diagram

Uploaded by

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

COIT 415

Chapter III – The Class Diagram

1
Chapter III
Class Diagram
1. Objects 8. Generalization
9. Super-Class Definition
2. Class
10. Sub-Class Definition
3. Attribute
11. Abstract Class
4. Operation 12. Concrete Class
5. Interface 13. Discriminator
6. Implementation 14. Polymorphism
7. Relations and Links 15. Static/Dynamic Binding
Association 16. Attribute and Operation
Visibility
Aggregation
17. Attributes in details
Composition 18. Operations in details
19. Types of classes
Object
What is an object?
1. any abstraction that models a single thing
2. a representation of a specific entity in the real world
3. may be tangible (physical entity) or intangible
Examples: specific citizen, agency, job, location,
order, etc.

3
A More Formal Definition
Attributes

An object is an entity with a


well-defined boundary and
identity that encapsulates
state and behavior.
State is represented by
attributes and relationships.
Behavior is represented by
operations, methods, and state
machines. Object

Operations
Object Definition Printer

- Serial number
Two aspects: - Model
- Speed
Information:
- Memory
1. has a unique identity - Status
2. has a description of its structure
3. has a state representing its current condition

Behavior: - print file


1. what can an object do? - stop printing
- empty the queue
2. what can be done to it?

5
Class
What is a class?
1. Any uniquely identified abstraction of a set of logically
related instances that share similar characteristics
2. Rules that define objects
3. A definition or template that describes how to build an
accurate representation of a specific type of objects
Examples: agency, citizen, car, etc.
Objects are created using class definitions as
templates
A class is a set of objects that share a
common structure and a common
behavior

6
Class Example

Class Example

7
Classes – Example
Class
Attributes
Account
+Owner: Person
+Ammount: double
+suspend()
+deposit(sum:double)
+withdraw(sum:double) Operations

8
Classes and Objects –
Example
Class Object AhmadAccount
+Owner=“Ahmad Saad"
+Ammount=5000.0
Account
+Owner: Person
+Ammount: double Object AliAccount
+suspend() +Owner=“Ali Zaki"
+deposit(sum:double) +Ammount=1825.33
+withdraw(sum:double)

FatmaAccount
Object +Owner=“Fatma Salah"
9
+Ammount=2557.0
Attribute
Attribute is a named property of a class describing a
range of values that instances of the class may hold for
that property
An attribute has a type and defines the type of its
instances
Only the object is able to change the values of its own
attributes
The set of attribute values defines the state of the
object
Examples: length, height, weight, color, date of birth,
cost etc.
Apr 10, 2024 10
Attribute

Attribute Example
11
Operation
 Operation is the implementation of a service that can be
requested from any object of a given class
 An operation could be:
1. a question - does not change the values of the attributes
2. a command – may change the values of the attributes

Whereas attributes describe


static characteristics of objects,
methods describe the behavior
of an object, that is, the services
that an object can provide
Each method has a name and a
body
Operation Example
12
Messages
object: sender
object: receiver
attributes:
attributes:

operations: message: [sender,


return value(s)]
operations:
message: [receiver,
operation, parameters]

Messages are the mechanism by which objects


(classes) interact.
Relations and Links
Relationships:
between classes (relations)
between objects (links)
Kinds of relationship :
1. Association (between classes)
2. Links (between objects)
3. Aggregation (between objects)
4. Composition (between objects)
5. Generalization (between classes)
6. Dependency (between classes)

14
1-Association
The simplest form of relation between classes
Peer-to-peer relations

Association is a set
of structural
relationships
among classes

15
1-Association - example

Initial Class model of the Library system


16
1-Association
Association Name

Professor Works for University

Association
Optional
Role Names
Class
Professor University

Employee Employer

17
1-Association
• It is rendered as a solid line.
• It may have a name & a role.
Name Direction
ASSOCIATION NAMES
Name
Works for
Person Company

Association

Association Role Name


ROLES
Works for
Person Company

Employee Employer
18
Role name
On occasions it is necessary to clarify the role played
by a class in an association.

19
Association: Multiplicity and
Navigation
Multiplicity defines how many objects participate in a
relationship
The number of instances of one class related to ONE
instance of the other class
Specified for each end of the association

Associations and aggregations are bi-directional by


default, but it is often desirable to restrict navigation to
one direction
If navigation is restricted, an arrowhead is added to
indicate the direction of the navigation
Multiplicities for Classes
Shows how many objects of one class can be
associated with one object of another class

Example: a citizen can apply for one or more licenses,


and a license is required by one citizen.

21
Multiplicities for Classes

22
Association: Multiplicity
Unspecified
Exactly one 1
Zero or more 0..*
 Many or unlimited *
One or more 1..*
Zero or one 0..1
Specified range
2..4
Multiple, disjoint ranges
2, 4..6
separated by ‘,’
2-Links
A link is defined as a physical or conceptual connection
between objects
An object collaborates with other objects through its links
A link is a specific form of an association through which an
object takes the services of another object
During implementation Links will become pointers or
references to objects.
A Link is an instance of an association and both are bi-
directional. (e.g., employs and employed by)
Where ambiguity appears, a directional arrow can be added
to clarify an Association or Link name.

24
3-Aggregation
 A restrictive form of “part-of” association
 Objects are assembled to create a more complex object
 Assembly may be physical or logical
 Defines a single point of control for participating objects
 The aggregate object coordinates its parts

It is a association which denotes a


whole/part relationship among
objects with the ability to navigate
from the whole (aggregate) to its
parts (attributes)

Apr 10, 2024 25


3-Aggregation Example
A CPU is part of a computer.
CPU, devices, monitor and keyboard are assembled to
create a computer.

26
3-Aggregation Example
Study pack
Course title
Number
Year
Instructor

1 1 1 1

1 ..* 1 ..* 1 ..* 0 ..*

Assignment OHP slides Lectur e Videotape


notes
Credi ts Sl ides Text Tape ids.

1 1

1 ..* 1 ..*
Exercises Solutions
#Problems Text
Description Diagrams

27
4-Composition
A stricter form of aggregation
Lifespan of individual objects depend on the on
lifespan of the aggregate object
Parts cannot exist on their own
There is a create-delete dependency of the parts to
the whole

28
4-Composition Example
A word cannot exist if it is not part of a line.
If a paragraph is removed, all lines of the paragraph
are removed, and all words belonging to that lines are
removed

29
5-Generalization
A process of organizing the features of different kinds of
objects that share the same purpose
Equivalent to “kind-of” or “type-of” relationship
Generalization enables inheritance
Specialization is the opposite of generalization

Generalization also called


Inheritance or Specialization

30
Aggregation

Composition

31
5-Generalization Example
Common features are defined in User.
FrontOfficeEmployee and BackOfficeEmployee
inherit them.

32
A generalisation hierarchy
Employee

Ma nager Programmer

budgetsControlled project
progLanguage
dateAppointed

Project De pt. Strategic


Ma nag er Ma nager Ma nag er
projects dept responsibilities

33
6-Dependency
• A semantic relationship between two model elements
where a change in one may cause a change in the other
• A dependency is shown as a dashed arrow ,the element
at the tail of the arrow depends on the element at the
arrowhead

Class Client Supplier

Dependency relationship

34
Super-Class Definition
Super-Class is a class that contains the features
common to two or more classes
A super-class is similar to a superset, e.g. agency-staff

35
Sub-Class Definition
Sub-Class is a class that contains at least the features
of its super-class(es).
A class may be a sub-class and a super-class at the
same time, e.g. management-staff.

Apr 10, 2024 36


Abstract Class
A class that lacks a complete implementation – provides
operations without implementing some methods
It cannot be used to create objects; cannot be instantiated, but can
only be inherited
It has one or more Abstract Operations
Abstract Operation is an operation for which no method is
given
Abstract class is introduced to store a set of attributes and
methods common to several classes in one place
Typically defined to provide interfaces for significant operations
Also to provide implementations for common methods

37
Concrete Class
A class without abstract operations is called a
concrete class.
Has methods for all operations
Can be instantiated
Methods may be:
a) Defined in the class or
b) Inherited from a super-class

38
Abstract Class Example in Java
// the Figure class must be declared as abstract because it contains an abstract method

public abstract class Figure


{
// because this is an abstract method the body will be blank
public abstract float getArea();
}

public class Circle extends Figure


{
private float radius;

public float getArea()


{ return (3.14 * (radius * 2)); }
}

public class Rectangle extends Figure


{
private float length, width;

public float getArea()


{ return length * width; }
} 39
Discriminator
Discriminator – an
attribute that defines sub-
classes
Example: “status” of agency
staff is a possible
discriminator to derive
“management”, “senior”
and “junior” sub-classes

40
Polymorphism
Ability to dynamically choose the method for an
operation at run-time or service-time
Facilitated by encapsulation and generalization:
a) encapsulation – separation of interface from
implementation
b) generalization – organizing information such that the
shared features reside in one class and unique features in
another
Operations could be defined and implemented in the
superclass, but re-implemented methods are in
unique sub-classes.

41
Polymorphism Example
Many ways of doing the same thing!
Example: management-staff and agency-staff can
apply for leave, but possibly in different ways

42
Polymorphism –
Example
A subclass may override the implementation of an operation it inherits from a superclass

Abstract Figure Abstract


class operation
+calcSurface() : double

Concrete
class
Square Circle
-x : int -x : int Override
-y : int -y : int operation
Override -size : int -radius : int
operation

Square::calcSurface() { Circle::calcSurface() {
return size * size; return PI * radius *
} raduis;
} 43
Static and Dynamic Binding
Strong typing and static typing are entirely different
Strong typing refers to type consistency

Static typing(Static binding) refers the time when names are


bound to types; also known as early binding
Def. Static binding means that the types of all variables and
expressions are fixed at compile time
Def. Dynamic binding means that the types of all variables
and expressions are not known until runtime; also known
as late binding

44
Static and Dynamic Binding
A binding is static if it occurs before run time and remains
unchanged throughout program execution
A binding is dynamic if it occurs during execution or can
change during execution of the program

As binding time gets earlier:


efficiency goes up
safety goes up
flexibility goes down

45
Static and Dynamic
Binding

46
Attribute and Operation Visibility
Attributes and operations can be given a visibility level
shown on the class diagram by a visibility indicator.
The visibility can be defined by either a keyword or a
symbol as follows:
Keyword Private Public Protected package
Symbol - + # ~
The visibility relates to its availability to other classes.
• Private(-):available only within the class that owns that feature
• Public(+): available to any class associated with the class that owns that
feature
• Protected(#): available within the class that owns that feature and any
subtype of that class
• Package(~): available only to other classes in the same package as the
declaring class
47
Attributes in details
There are three properties of attributes :
- Default values Multiplicity
- Derived attributes
- Multiplicity Employee
- EmpId : String
- LastName : String
- MiddleName [0..2] : String
Derived attribute - FirstName : String
- DateOfBirth : Date
- /age : integer
- dateRegistered : Date = today
{ age = today – - GPA : float = 0
DateOfBirth }
+ InputEmpData ()

Default Value
48
Attributes in details
For example:
telephoneNumber[1..3]:at least one value and up to 3
telephoneNumber[0..1]: null or one value
telephoneNumber[1..*]:at least 1 value, up to unlimited

We can add properties to the attributes


telephoneNumber[1..3]{ordered}: preferred sequence
telephoneNumber[1..3]{unique}:no duplicated value
telephoneNumber[1..3]{ordered ,unique}

49
Operations in detail
Each parameter in the list conforms to the following syntax:
parameterName : parameterType

Example:
setNextEmp(EmpId : String)

Omitting the parameterName is valid


setNextEmp(String)

A parameter can have a default value


approveApplication(dateApproved : Date = today)

50
Operations in detail
Parameters passed by value
Int x;
x = 57 ;
doIncrement (in x) ;
print(“In main”) ;
print (x)
doIncrement (p: Int) {
p=p+1 ;
print (“In doIncrement”);
print (p) ;
}
In doIncrement
58
In main
57
51
Operations in detail
Parameters passed by reference
Int x;
x = 119 ;
doIncrement (inout x) ;
print(“In main”) ;
print (x)
doIncrement (p: Int) {
p=p+1 ;
print (“In doIncrement”);
print (p) ;
}
In doIncrement
120
In main
120
52
Object instance
Basic object notation are:
Similar to basic class notation
Name compartment of the object shows the name of
object instance and its class type.
Form: objectName:ClassName ( all underlined)
 For example: aDocument:Document, saledReport:Document

aDocument:Document salesReport:Document

53
Stereotypes for Classes
Attribute or operation lists in a class may be organized into
groups with stereotypes.
A number of stereotypes of classes and data types:
1. Thread
2. Event
3. Entity
4. Process
5. Utility
6. Metaclass
7. Powerclass
8. Enumeration, etc.

54
Types of Classes
Abstract class VS concrete class
Super-class VS sub-class
Root class VS leaf class

1) abstract class
cannot have direct instances
the name is written in italics
2) root class
cannot be a sub-class
written with root stereotype
3) leaf class
cannot be a super-class
written with leaf stereotype

55
Types of Classes
Icon
base class
{root}
Origin : Point
diaplay() abstract operation
getID() : Integer {leaf}
abstract class
concrete operation
RectangularIcon ArbitraryIcon
height : Integer edge : LineCollection
width : Integer
isInside(p : Point) : Boolean

Button

display() concrete class polymorphic operation

OKButton
{leaf}

display()

leaf class 56

You might also like