ITCS 114
Classes inUML
• A class is a description of a set of objects
that share the same attributes, operations,
relationships, and semantics.
• Graphically, a class is rendered as a
rectangle, usually including its name,
attributes, and operations in separate,
designated compartments.
ClassName
attributes
operations
3.
ITCS 114
Class Names
Thename of the class is the only required tag in the graphical
representation of a class. It always appears in the top-most
compartment.
ClassName
attributes
operations
4.
ITCS 114
Class Attributes
Anattribute is a named property of a
class that describes the object being modeled.
In the class diagram, attributes appear in
the second compartment just below the
name-compartment.
Attributes are usually listed in the form:
attributeName : Type
Person
name : String
ID : long
5.
ITCS 114
Class Attributes(Cont’d)
Person
- name : String
- ID : Long
Attributes can be:
+ public
- private
6.
ITCS 114
Class Operations
Person
-name : String
- ID : Long
setPerson
getName
getID
Operations describe the class behavior
and appear in the third compartment.
7.
ITCS 114
• Youcan 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.
• Constructors should not be included in
UML class diagram
Class Operations (Cont’d)
Person
- name : String
- ID : Long
+setPerson( String, Long): void
+getName( ) : String
+getID( ): Long
8.
ITCS 114
UML Objects(instances)
• Specify object (instance)
name and the class name
• For each attribute show its
value
• Do not show methods
Employee: Person
name = “Eman”
ID = 589651
Object name: Class Name
Attribute name= value
Student: Person
name = “Ahmed”
ID = 123456
Examples of Objects of type Person
Person
- name : String
- ID : Long
+setPerson( String, Long): void
+getName( ) : String
+getID( ): Long
Class
9.
ITCS 114
Exercise- Book
Designa class to represent a book. Think about the attributes of a
book; that is, what data is in the book? What behaviours might be
reasonable for a book? Use the answers to these questions to write a
UML class diagram for a book class. Then give two examples of
instances of this class.
10.
ITCS 114
Exercise- Book
Book
-title: String
- authorName: String
- publisher: String
- numCopies: int
+setBook( String, Long, int): void
+setTitle(String): void
+setAuthor(String): void
+setPublisher(String): void
+incrementNumCopies(int): void
+getName( ) : String
+getID( ): Long
+getNumCopies: int
JavaBook: Book
Title “= Java Book”
authorName = “Smith”
Publisher = “Springer”
numCopies = 100
C++Book: Book
Title = “Introduction to C++”
authorName = “Han”
Publisher = “Elsevier”
numCopies = 25
Examples of Objects of type Book
Book UML Class
11.
ITCS 114
Exercise- Rectangle
Designa class to represent a Rectangle. Think about the attributes and
methods of a rectangle? Use the answers to these questions to write a
UML class diagram. Then give two examples of instances of this class.
ITCS 114
Test- Question
Question4 ( 6 points)
Design a class to represent a Product. The main attributes of this class
are: Name, Price and Code. Draw a UML class diagram with the given
attributes and suitable methods. Then show one instance of this class.
14.
ITCS 114
Test –Question –
Answer
Product UML Class
An Object Example of type Product