You are on page 1of 11

CSE 3421

UML Class Diagram

SUMMER 2021
MD. RAFI-UR-RASHID
LECTURER, DEPT. OF CSE, UIU
Why This Lesson?
It is a crucial task in OOP
What is a UML class diagram?
• A UML class diagram is a picture of the classes in an OO system
• their fields and methods
• connections between the classes that interact or inherit from each other

• Not represented in a UML class diagram:


• details of how the classes interact with each other
• algorithmic details; how a particular behavior is implemented

4
Class attributes (fields, instance variables)
visibility name : type [count] = default_value
Student
• Visibility +
+
name: int
email: String
+ public - DOB: String
/ Age: int
# protected # ID: int
~ courses[100]:Course
- private
~ package (default) +Student(n:String,dob:String)
/ derived + getTotalCredits():Course
• Method listed as name: return type # calculateTuition():double
+ calculateGPA(crs:Course[]):float
• Parameters listed as name: type
• Omit return type on constructors
and when return type is void
5
Diagram of a single class Class name on top
• write «interface» on top of interfaces' names
• use italics for an abstract class name

Student Rectangle <<interface>>


Shape
- name: String - width: int
- id: int - height: int
- totalStudents:int / area: double

# getID():int # Rectangle(w:int, h:int)


~ getName():String + distance(r:Rectangle):double + calculateArea():double

Operations/ methods (optional)


• may omit trivial (get/set) methods
• but don't omit any methods from an interface!
• should not include inherited methods

6
Relationships between class
• Generalization: an inheritance relationship
• inheritance between classes
• interface implementation
• Association: a usage relationship
• dependency
• aggregation
• composition

7
Generalization relationships <<interface>>
Shape

• Hierarchies drawn top-down + calculateArea():double

• Arrows point upward to parent


RectangularShape
• Line/arrow styles indicate if parent is - width:int
- height: int
a(n): / area: double
+ calculateArea():double
• class: solid line, black arrow + contains(x:int,y:int):bool

• interface: dashed line, white arrow


• abstract class: solid line, white arrow
Rectangle

+ Rectangle(x:int,y:int)
+ distance(r:Rectangle):double
8
Association (usage) relationships

Class A 3…* 1 Class B

9
Association multiplicities
One to one One to many
 Each car has exactly one engine  Each book has many pages
 Each engine belongs to exactly one car  Each page belongs to exactly one book

Engine Car Page Book


1 * 1
1

Multiplicity (how many are used)


• * (zero or more)
• 1 (exactly one)
• 2..4 (between 2 and 4, inclusive)
• 3..* (3 or more, * may be omitted)
10
Association types
Aggregation: “is part of” Engine Car
1 1
 symbolized by a clear white diamond

Composition: “is entirely made of”


 stronger version of aggregation Page Book
 the parts live and die with the whole * 1
 symbolized by a black diamond

Dependency: “uses temporarily”


Lottery Random
 symbolized by dotted line
 often is an implementation detail, not an
intrinsic part of the object's state

11

You might also like