You are on page 1of 21

Object Oriented Programming

1
Today’s Topics

• Classes in UML
• Relationships
– Association
– Dependency
– Generalization
• Class Diagrams
• Advanced Features

2
Classes

In UML a class is represented as a rectangle


Shape attributes
origin
move()
name
resize()
Display() operations

3
Classes
• Attributes
• a property of a class. It describes a range of values
that the property may hold in objects of the class. A
class may have zero or more attributes.

Rectangle
height : Float
width : Float
isFilled : Boolean = false

4
Classes
• Operations.
• An operation is something that a class can do, or that
you (or another class) can do to a class.

Rectangle

add()
move( x : Integer, y : Integer )
isEmpty() : Boolean

5
Class Diagrams
• Used to model the static view of a system
• Contents
– Classes
– Interfaces
– Collaborations
– Dependency, Generalization and association
relationships
– Packages or Subsystems ( for grouping )

6
Different Ways of Using Class Diagrams

• To model the vocabulary of a system


• To model simple collaborations
• To model a logical database schema

7
Class Diagrams
• Suppose that you're writing a document in some of famous
text processing tools, like MSWord for example. You can
start typing a new document, or open an existing one. You
type a text by using your keyboard. Every document consists
of several pages, and every page consists of header,
document's body or/and footer. In header and footer you may
add date, time, page number, file location e.t.c.Document's
body has sentences. Sentences are made up of words and
punctual signs. Words consists of letters, numbers and/or
special characters. Also in the text you may insert pictures
and tables. Table consists of rows and columns. Every cell
from table may hold up text or pictures.

8
Class Diagrams
document, text processing tool,
MSWord, text,
keyboard, header,
footer, document's body,
date, time,
page number, location of file,
page, sentence, word,
punctual sign, letter, number,
special character, picture, table,
row, column, cell,
user

9
Advanced Structural Modeling
• Necessary to provide more specific details in the
model
• Useful in implementing the model
• Features
– Visibility
– Scope
– Abstract Classes
– Root / Leaf Classes
– Multiplicity
– Attributes / Operations

10
Classifiers
• A classifier is a mechanism that describes structural and
behavioral features
• Classes are a kind of classifiers
• Other kinds of classifiers include,
– Interfaces
– Data Types
– Use Cases
– Sub Systems
– More
• Advanced features of UML discussed here can be applied
to any classifier, not just classes

11
Visibility
• Specifies whether an attribute or operation
can be used by another class
• 3 Levels of visibility
• Public
– Any outside class with visibility to the given
class can use it
Student
+name
+changeCourse()
12
Visibility
• Protected
– Any descendant of the class can use the feature
– Symbol: #
• Private
– Only the class itself can use it
– Symbol: -

13
Visibility

Eg:- public class Employee


{
Employee
#name
protected String name;

-salary private double salary;

+giveIncrement()
#calculateIncrement() public String getName()

+getName() : String {
return name;
}
}
14
Scope
• Instance
– Each instance of the class holds its own value
for the feature ( attribute, operation )
• Classifier
– There is just one value of the feature for all
instances of the classifier
class Student
Student
#studentCount:Integer
{
-name
protected static int studentCount;
} 15
Abstract Classes

• Classes that may not have any direct


instances
Abstract
<<interface>>
Shape
Polygon
coordinates draw()
draw()
public abstract class Shape
{
Rectangle
}
16
Root, Leaf Classes
public abstract class Shape
Shape
{
{root}
public Point position;
position
public abstract void draw();
draw()
}
public final class Rectangle
extends Shape
Rectangle
{
{leaf} public void draw(){}
draw()
}

17
Multiplicity
• The number of instances a class may have
• Can be applied to attributes as well

singleton class
Registrar 1

regFile [1..*] : File


AssistantRegistrar 2

multiplicity

18
Attributes
• The full form for defining an attribute
[visibility] name [multiplicity] [:type]
[ = initial-value ] [ {property-string}]
• Where property string is one of
– changeable
– addOnly
– frozen ( constants in Java – final keyword)

19
Operations
• The full form for defining an operation
[visibility] name [ ( parameter-list ) ]
[: return-type] [ { property-string } ]
• Each parameter is defines as
[ direction ] name : type [ = default-value ]

+ updateSalary ( inout emp : Employee, in salary : Double = 1000.0 )


{leaf}

20
Reference

• The Unified Modeling Language User


Guide
• Grady Booch, James Rumbaugh, Ivar Jacobson
– Chapters 8, 9

21

You might also like