Introduction To OO Concepts and UML
Introduction To OO Concepts and UML
UML
1
Overview
Introduction to Classes and Objects
Introduction to UML
Data Abstraction
Inheritance
Polymorphism
Association
Persistence
2
From Procedural to OO
All programming languages support four basic concepts.
Calculation – constants, variables, operators, expressions
3
Abstraction in Procedural Languages
return result;
}
4
Abstraction in OO Languages
Abstraction in OO languages is provided through an abstract data
type (ADT), which contains data and procedures that operate on
data.
Abstract Data Type (ADT) is a definition that contains data and
procedures that operate on the data.
In Java, class is an implementation on an abstract data type.
In OO terminology;
data is referred to as fields, parameters or attributes
5
Account Class Example
Account
account name
Data
account number
withdraw cash
6
Account Class
// Pseudo Code for Account class
class Account {
String accountName;
double accountBalance;
withdrawCash();
depositCash();
checkBalance();
} // Class Account
7
Circle Class Example
Circle
centre
Data
radius
area
circumference Methods
move
8
Circle Class
// Pseudo Code for Circle class
class Circle {
double cetreX, centreY;
double radius;
area();
circumference();
move();
} // Class Circle
9
Class vs Object
10
Examples of Objects
11
Classes: Objects with the same
attributes and behavior
Person Objects
Vehicle Objects
Polygon Objects
Polygon Class
Abstract Attributes: Vertices, Border,
Into Color, FillColor
Operations: Draw(), Erase(), Move()
13
UML History
1994 – Grady Booch and Jim Rumbaugh started at Rational
creating the new notation
Grandy Booch – Booch Method
Jim Rumbaugh – Object Modeling Technique
15
Object Oriented Paradigm: Features
Encapsulation
Data Abstraction
Inheritance
Polymorphism
Association
Persistence
16
Encapsulation
All information (attributes and methods) in an object oriented
system is stored/hidden within objects/classes.
17
Encapsulation - Example
message
wi
)
th
h(
dr
as
aw
messag itC accountN
Ca
s
po
ame
e
s
de
h(
accountBa
)
lance
checkBalance()
message
18
Encapsulation - Example
class Account {
private String accountName;
private double accountBalance;
public withdrawCash();
public depositCash();
public checkBalance();
} // Class Account
19
Data Abstraction
The technique of creating new data types that are well suited to
an application.
done by defining new classes.
Example : Creating new classes Account and Circle creates new data
types Account and Circle that can be used any application.
20
Abstraction - Example
class Account {
private String accountName;
Creates a data
private double accountBalance;
type Account
21
Inheritance
New data types (classes) can be defined as extensions to
previously defined types.
Parent Class (Super Class) – Child Class (Sub Class)
Subclass inherits properties from the parent class.
Paren
t
Inherited
capability
Child
22
Inheritance - Examples
Account Class
Account class in our example had two attributes :
23
Inheritance in UML
CheckingAccount is a subclass (inherited from ) of a parent class
– Account.
Account
CheckAccount
24
Inheritance - Examples
Circle Class
Circle class in our example has attributes centre and radius.
and methods.
Define a parent class Shape.
Move common properties to class Shape.
Circle class and Rectangle class can be then be defined as
sub classes of Shape class.
25
Uses of Inheritance - Reuse
26
Uses of Inheritance - Specialisation
27
Uses of Inheritance – Common Interface
Not all operations supported for Rectangle and Circle are the
same.
implemented in parent.
circumference(), area() operations are significantly different
28
Uses of Inheritance - Extension
29
Uses of Inheritance – Multiple Iinheritance
Graphics Shape
Circle
30
Uses of Multiple Inheritance
This is done when a class resides in more than one inheritence
heirarchy.
The class inherits behaviour from multiple parent classes.
Eg. Circle class can inherit move() from the Shape class and
paint() from the Graphics class.
31
Polymorphism
Polymorphic which means “many forms”
Poly – many
Morphos - forms.
32
Polymorphism
An object of type Circle or Rectangle can be assigned to a Shape
object. The behaviour of the object will depend on the object
passed.
33
Polymorphism – Method Overloading
34
Polymorphism – Operator Overloading
35
Association
A class can maintain a relationship with another class which will
allow the class to communicate with the other class.
This type of relationship is called an association.
Example :
contains> Rectangle
36
A simple class diagram
A Class diagram shown classes and their relationships.
teacher.
37
Why OOP?
Greater Reliability
Break complex software projects into small, self-contained, and
modular objects
Maintainability
Modular objects make locating bugs easier, with less impact on
38