Chapter3 Class Diagram
Chapter3 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
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
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
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
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
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
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
26
3-Aggregation Example
Study pack
Course title
Number
Year
Instructor
1 1 1 1
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
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
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
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.
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
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
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
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
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
49
Operations in detail
Each parameter in the list conforms to the following syntax:
parameterName : parameterType
Example:
setNextEmp(EmpId : String)
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
OKButton
{leaf}
display()
leaf class 56