Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 38

Introduction to OO Concepts and

UML

1
Overview
 Introduction to Classes and Objects

 Introduction to UML

 Features of Object Oriented Paradigm


 Encapsulation

 Data Abstraction

 Inheritance

 Polymorphism

 Association

 Persistence

2
From Procedural to OO
 All programming languages support four basic concepts.
 Calculation – constants, variables, operators, expressions

 Selection - if-else, switch, ?


 Iteration - while, do, for

 Abstraction – The process of creating self-contained units of

software that allows the solution to be parameterized and


therefore more general purpose.

 Abstraction is the fundamental concept that differentiates


procedural programming languages such as C from OO languages
such as Java, C++.

3
Abstraction in Procedural Languages

 Abstraction in procedural languages is provided through functions


or procedures.
 Functions – modify external data by performing a particular
operation.
e.g. A c function to calculate the average of two numbers.

float calculate_average (float a, float b) {


float result;
result = (a + b)/2;

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

 procedures are referred to as methods or operations

 A class contains attributes and methods that operate on the


attributes.
 A class is a generalization of a real world entity that is used in a
OO program depending on the application.
e.g. animal, student, car, account etc.

5
Account Class Example

Account

account name
Data
account number

withdraw cash

deposit cash Methods

check account balance

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

 An Object is an instance of a class.


e.g. The object: Zahid’s Account is an instance of Account class.
The object: Zahid’s Account is another instance of the
Account class.

 Objects hold state information while classes do not.


e.g. Zahid’s Account
Account Name = Zahid
Account Balance = 15,000

 A class represents a template for several objects that have


common properties.

 An Object Oriented system is a collection of interacting Objects.

10
Examples of Objects

CAR BOY GIRL CLOCK

VDU BOOK TREE TRIANGLE

Figure 1.9: Examples of objects

11
Classes: Objects with the same
attributes and behavior
Person Objects

Abstract Person Class


Into Attributes: Name, Age, Sex
Operations: Speak(), Listen(), Walk()

Vehicle Objects

Abstract Vehicle Class


Into Attributes: Name, Model, Color
Operations: Start(), Stop(), Accelerate()

Polygon Objects

Polygon Class
Abstract Attributes: Vertices, Border,
Into Color, FillColor
Operations: Draw(), Erase(), Move()

Figure 1.12: Objects and classes 12


Introduction to UML
 UML – Unified Modeling Language
 A graphical modeling language that can be used to represent
artifacts of object oriented analysis, design and implementation.

 It is a standard that has international support.

 Was developed by Rational Software –


 Grady Booch, Jim Rumbaugh and Ivar Jacobson

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

 1995 – Ivar Jacobson Joined the team


 Object-Oriented Software Engineering

 First Version of UML – 0.8

 1995 Object Management Group (OMG) agreed to make UML


the standard.

 1996 – Additional companies got involved.

 Current version of UML is 2.0


14
UML Notations
 In the next few weeks, UML notation will be introduced along
with java to represent Object Oriented concepts.
And That’s your Assignment

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.

 Information can be manipulated through operations performed on


the object/class – interface to the class. Implementation is hidden
from the user.

 Objects support Information Hiding – Some attributes and


methods can be hidden from the user.

 This information hiding capability is called Encapsulation.

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

 Only methods withdrawCash(), depositCash() and checkBalance()


have access to (can read or modify) accountName and
accountBalance.

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.

 It allows the creation of user defined data types, having the


properties of built in data types and more.

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

public withdrawCash(); Account acctX;


public depositCash(); Account acctY;
public checkBalance();
} // Class 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 :

accountName and accountBalance.


 A check account:

 has attributes accountName and accountBalance.


 In addition: number of cheques issued (numChecks). This

attribute may not be applicable to all types of accounts.


 Instead of creating a new class CheckAccount which has all

three attributes, inheritence allows CheckAccount to be defined


as a sub class of Account class
 thus inheriting attributes accountName and
accountBalance from the Account class.
 Account is the Super Class and CheckAccount is the Sub Class.

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.

 Consider a Rectangle class. Can have an attribute centre.

 However, radius in not an applicable attribute.

 Rectangle class should have attributes height and width.

 Height and width not applicable to circle.

 The methods area(), circumference() and move() defines in

the Circle Class are applicable to the Rectangle class.


 Do not define a new class Rectangle holding all three attributes

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

 If multiple classes have common attributes/methods, these


methods can be moved to a common class - parent class.

 This allows reuse since the implementation is not repeated.

Example : Rectangle and Circle method have a common method move(),


which requires changing the centre coordinate.

26
Uses of Inheritance - Specialisation

 Specialised behaviour can be added to child classes.

 In this case the behaviour will be implemented in the child class.


 E.g. The implementation of area() method in the Circle class is

different from the Rectangle class.

 area() method in the child classes override defintions of the


method in parent classes.

27
Uses of Inheritance – Common Interface
 Not all operations supported for Rectangle and Circle are the
same.

 Some methods have common implementation and others don’t.


 move() operation is common to classes and can be

implemented in parent.
 circumference(), area() operations are significantly different

and have to be implemented in the respective classes.

 The Shape class provides a common interface where all 3


operations move(), circumference() and area().

28
Uses of Inheritance - Extension

 Extend functionality of a class.


 Child class adds new operations to the parent class but does not
change the inherited behaviour.

 E.g. Rectangle class might have a special operation that may


not be meaningful to the Circle class - rotate90degrees()

29
Uses of Inheritance – Multiple Iinheritance

 Inherit properties from more than one class.


 This is called Multiple Inheritance.

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.

 Multiple inheritance is not supported in JAVA but is supported in


C++.

31
Polymorphism
 Polymorphic which means “many forms”
 Poly – many

 Morphos - forms.

 In OOP, polymorphism itself has many forms.

 Polymorphism allows a single object, method, operator to be


defined differently depending on the type of data passed to it.

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.

circleA = new Circle(); Create a new circle object


Shape shape = circleA;
shape.area(); area() for circle class will be executed

rectangleA = new Rectangle(); Create a new rectangle object


shape= rectangleA;
shape.area() area() method for rectangle will be executed.

33
Polymorphism – Method Overloading

 Multiple methods can be defined with the same name, accepting


different input arguments.
Method 1 - initialize(int a)
Method 2 - initialize(int a, int b)

 The Appropriate method will be called based on the input


arguments.
initialize(2) Method 1 will be called.
initialize(2,4) Method 2 will be called.

34
Polymorphism – Operator Overloading

 Allows regular operators such as +, -, *, / to have different


meanings based on the type.

 E.g. + operator for Circle can re-defined


Circle c = c + 2;
 Not supported in JAVA. C++ does support it.

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 :

The Circle and Rectangle objects may be contained in a Diagram


class. This creates and association between the Diagram class and
Circle class and also Diagram and Rectangle class.
contains>
Circle
Diagram

contains> Rectangle

36
A simple class diagram
 A Class diagram shown classes and their relationships.

 Draw a simple class diagram for the following description.


 A student can be an undergraduate or a graduate.

 An undergraduate student can be a type of tutor.

 A tutor tutors a student.

 A teacher and a professor are two types of instructors.

 A teacher assistant is a type of graduate student who assists a

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

the overall project


 Greater Productivity through Reuse!
 Faster Design and Modelling

38

You might also like