






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to Object-Oriented Programming (OOP) using class diagrams in the Unified Modeling Language (UML). It covers the basics of classes, attributes, methods, and associations, including visibility, multiplicity, and navigability. Students will learn how to represent the structure of an OO solution using UML class diagrams and understand the relationships between classes.
Typology: Lecture notes
1 / 11
This page cannot be seen from the preview
Don't miss anything!







Instead of writing our own handout, we refer you to the document Object-Oriented Programming with Objective-C released by Apple Inc. This is compulsory reading for those who are new to Object Oriented (OO) Programming. In spite of the title, the document is mostly programming language independent; you may ignore any references to Objective C.
Class diagrams UML class diagrams describe the structure (but not the behavior) of an OO solution. These are possibly the most often used diagrams in the industry and an indispensable tool for an OO programmer.
Class name visibility name = default-value … visibility name (parameter-list) : return-type …
attributes
methods
Table
The above illustrates the basic notations of a class diagram. In particular, attributes represent the data of the class, as opposed to methods that represent the operation (or behavior).
Visibility The visibility of attributes and operations is used to indicate the level of access allowed for each attribute or operation. The types of visibility (and their exact meaning) depend on the programming language used. Here are some common visibilities and how they are indicated in a class diagram:
Associations Within an OO solution, objects collaborate with one another. These collaborations are represented as connections (or associations ). Each association is denoted as a line between two classes in a UML class diagram. As an example, the Admin object needs to be associated to Student objects, so as to access information. The association is depicted as follows.
Admin (^) students Student
Class A Attributes
UML Notation : Association (partial) Class B Attributes
[role of B] Operations [role of A] Operations
Note that an association link in a class diagram denotes a permanent or semi-permanent structural link between objects of two classes. Temporary contacts between objects (e.g. ClassA uses a constant defined in ClassB) are not shown as associations. Such temporary contacts are shown as dependencies instead (denoted as a dashed arrows).
Class A Attributes
UML Notation : Dependency Class B Attributes Operations Operations
The ‘Operations’ compartment (or even both ‘Attributes’ and ‘Operations’ compartments) may be omitted if such details are not important for the task at hand. For simplicity, these two compartments are omitted from most of the subsequent class diagrams.
Class A Attributes
Class B Attributes Operations
Class C Class E Operations
Class D Operations
‘Role’ labels are optionally used to indicate the role played by the classes in the relationship. For example, the association given below represents a marriage between a Man object and a Woman object. The respective roles played by objects of these two classes are ‘husband’ and ‘wife’.
Man wife Woman husband
A ssociation labels are used to describe the association. In the diagram below, “an Admin object uses Student objects” or “Student objects are used by an Admin object”. The arrow head indicates the “direction” in which the label is to be read.
Admin staff* Professor (^1) supervisor student0..5 Student students
UML notes and constraints UML notes are used to augment a UML diagram with additional information. These notes can be shown connected to a particular element in the diagram or can be shown without a connection. The diagram below shows examples of both.
Admin staff* Professor (^1) supervisor student0..5 Student students
This may be redundant. To be verified later.
This diagram is only a work in progress.
A note can be used to specify a constraint within curly brackets. Natural language or a formal notation such as OCL (Object Constraint Language) may be used to specify constraints. OCL is beyond the scope of this handout.
Player
Die
Turn
FaceValue
takes
throws 1 1
{ FaceValue can be 1,2,3,4,5 or 6 only }
involves throwing
Navigability Navigability is another detail that can be shown in class diagrams. Navigability (denoted as an arrowhead in the association link) indicates whether a class involved in an association is "aware" of the other participating class.
Logic Minefield Cell
ConfigGenerator
Logic is aware of Minefield, but Minefield is not aware of Logic
At the level of program code, navigability implies how objects are referenced. In the example above, a Logic object holds a reference to a ConfigGenerator object, but not the other way around. Navigability can be unidirectional or bidirectional. For convenience, a line without arrow heads can be used to indicate a bidirectional link. However, if none of the associations in a class diagram has navigability arrows, it usually means that navigability is “unspecified”.
Logic Minefield
Unidirectional. Only one class is aware of the other.
UI Core
Bidirectional. Each class is aware of the other.
Either bidirectional or UI Core navigability unspecified.
Associations as attributes An attribute is sometimes used to represent an association. The diagram below depicts a multiplayer Square Game being played on a board comprising of one hundred squares. Each of the squares may be occupied with any number of Pieces, each belonging to a certain player. A piece may or may not be on a square. Note how the association ’is on’ can be replaced by an isOn attribute of the Piece class. In order to realize the 0..1 multiplicity, the isOn attribute can either be null or hold a reference to a Square object.
SquareGame Player
plays
Piece
Board Square number
owns
played on
100 has
is on
2..* 1
SquareGame Player
plays
Piece
Board Square number
owns
played on
100 has
2..* 1
isOn:Square
0.. 1
In addition, association multiplicities of two or more can be made as part of the attribute using
name: type [multiplicity] = default value
Board squares: Square[100]=null
Enumerations An <
Logic Logic Minefield (^) Same as mineField: Minefield
class Logic { Minefield minefield; … }
class Minefield { … }
A variable gives us a 0..1 multiplicity (also called optional associations ) because a variable can hold a reference to a single object or null. A variable can be used to implement a 1-multiplicity too (also called compulsory associations ). To implement other multiplicities, choose a suitable data structure such as Arrays, ArrayLists, HashMaps, Sets, etc.
Logic (^) ConfigGenerator
class Minefield { Cell[][] cells ; … }
Logic Minefield
0..
1
Minefield Cell
W*H
class Logic { Minefield minefield; … } class Logic { ConfigGenerator cg; … }
Class-level members Most OO languages allow defining class-level (also called static) attributes and operations. They are like ‘global’ variables/functions but attached to a particular class. For example, the variable totalStudents of the Student class can be declared static because it should be shared by all Student objects. However, the variable name should not be static as each Student should have its own name. Similarly, getTotalStudent() can be a static operation. In UML class diagrams, underlines are used to denote class-level attributes and variables.
Single Responsibility Principle The Single Responsibility Principle (SRP) states,
A class should have one, and only one, reason to change.
If a class has only one responsibility, it needs to change only when there is a change to that responsibility. A counter example is a TextUi class that does parsing of the user commands as well as interacting with the user. That class needs to change when the formatting of the UI changes as well as when the syntax of the user command changes. Hence, such a class does not follow the SRP. Refer the following article for a longer write up on SRP:
http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod
Encapsulation in OOP An object is an encapsulation some data and related functions. i.e.,
References [1] Object-Oriented Programming with Objective-C , A document by Apple inc., retrieved from http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/OOP_ObjC /OOP_ObjC.pdf
Worked examples [Q1] (a) Which of the following class diagrams match with the object diagram below? For example, class diagram (1) matches with the object diagram because the object diagram could be an instance of the class diagram given.
:Unit :Item
Unit 1 Item 1
Unit Item 1 0..
Group
(a)
(1) matches (2) matches (3) does not match – According to this model, there should be at least 2 Items per Unit. (4) matches (5) does not match – According to this model, a Unit must have a link to a Group.
(b)
(1) allowed (2) not allowed. One item is not linked to a Unit (3) Not allowed. An Item cannot be linked to more than one other Item. (4) Allowed. A unit can exist without an Item.
[Q2] Infer the class names that are missing in the following object diagram. Draw a class diagram
Organization Country State < isMemberOf < isPartOf
< borders
< borders
< borders
Note: This answer does not use inheritance as it was not covered in this handout.
[Q3] First, draw an object diagram to represent the following description. Then, use the object diagram to draw a class diagram. Be sure to indicate the multiplicity and label the associations.
Sichuan, Jiangsu, and Guandong are all provinces in China. Singapore, Malaysia, and China are all countries in Asia. Beijing is the capital of China. Kuala Lumpur is the capital of Malaysia.
[A3]
Sichuan:Province China:Country Guandong:Province Asia:Continent
Jiangsu:Province
Singapore:Country Malaysia:Country
Beijing:City
KualaLampur:City
belongs to >
belongs to >
belongs to >
< is capital of
is capital of >
is part of >
is part of >
is part of >
Province Country Continent
City
is part of > belongs to >
< is capital of
0..
1
0..
1