OOPS
OOPS
Oriented Design
1
Overview
4
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()
Data Abstraction
Single Inheritance
Polymorphism
OOP
Paradigm
Persistence
Delegation
Genericity
Multiple Inheritance
6
Java’s OO Features
Encapsulation
Data Abstraction
Single Inheritance
Polymorphism
OOP Java
Paradigm
Persistence
Delegation
Genericity
Multiple Inheritance
7
Encapsulation
Encapsulation
It associates the
Data Abstraction code and the data
it manipulates into
Single Inheritance
a single unit; and
Polymorphism keeps them safe
OOP
Paradigm from external
Persistence
interference and
Delegation
misuse.
Genericity Data
Functions
Multiple Inheritance
8
9
Data Abstraction
Encapsulation
The technique of creating
new data types that are
Data Abstraction
well suited to an
application.
Single Inheritance
It allows the creation of
user defined data types,
Polymorphism
OOP having the properties of
Paradigm built data types and a set
Persistence
of permitted operators.
In Java, partial support.
Delegation
In C++, fully supported
(e.g., operator
Genericity
overloading).
Multiple Inheritance
10
Abstract Data Type (ADT)
Class is an implementation of an
Abstract Data Type.
11
Class- Example
class Account {
private String accountName;
private double accountBalance;
public withdraw();
public deposit();
public determineBalance();
} // Class Account
12
Class
Student Circle
Account
13
Objects
14
Classes/Objects
16
Object
Objects have state and classes don’t.
John is an object (instance) of class Student.
name = “John”, age = 20, studentId = 1236
17
Encapsulation
All information (attributes and methods)
in an object oriented system are stored
within the object/class.
Information can be manipulated through
operations performed on the object/class
– interface to the class. Implementation is
hidden from the user.
Object support Information Hiding – Some
attributes and methods can be hidden
from the user.
18
Encapsulation - Example
class Account {
private String accountName; message
message
19
Data Abstraction
20
Abstraction - Example
class Account {
private String accountName;
Creates a data
private double accountBalance;
type Account
public withdraw();
public deposit(); Account acctX;
public determineBalance();
} // 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) Parent
Subclass inherits Inherited
capability
properties from the
parent class.
Child
22
Inheritance - Example
Example
Define Person to be a class
A Person has attributes, such as age, height, gender
Assign values to attributes when describing object
Define student to be a subclass of Person
A student has all attributes of Person, plus attributes
of his/her own ( student no, course_enrolled)
A student has all attributes of Person, plus attributes
of his/her own (student no, course_enrolled)
A student inherits all attributes of Person
Define lecturer to be a subclass of Person
Lecturer has all attributes of Person, plus attributes
of his/her own ( staff_id, subjectID1, subjectID2)
23
24
Inheritance - Example
Circle Class can be a subclass
(inherited from ) of a parent class -
Shape
Shape
Circle Rectangle
25
Inheritance - Example
Inheritance can also have multiple
levels. Shape
Circle Rectangle
GraphicCircle
26
Uses of Inheritance - Reuse
27
Uses of Inheritance - Reuse
Circle Rectangle
centre
centre height
radius width
area() area()
circumference() circumference()
move(newCentre) move(newCentre)
move(newCentre){
centre = newCentre; move(newCentre){
} centre = newCentre;
}
28
Uses of Inheritance - Reuse
Shape
centre move(newCentre){
centre = newCentre
area() }
circumference()
move(newCentre)
Circle Rectangle
height
radius width
area() area()
circumference() circumference()
29
Uses of Inheritance - Specialization
area(){
return pi*r^2; area(){
} return height*width;
}
31
Uses of Inheritance - Specialization
Shape
area(){
Circle Rectangle
return pi*r^2;
}
height
radius width
area() area(){
area()
circumference() return height*width;
circumference()
}
32
Uses of Inheritance – Common
Interface
All the operations that are 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().
33
Uses of Inheritance - Extension
34
Uses of Inheritance - Extension
Shape
centre
area()
circumference()
move(newCentre)
Circle Rectangle
height
radius width
area()
area()
circumference()
circumference()
rotate90degrees()
35
Uses of Inheritance – Multiple
Inheritance
Inherit properties from more than
one class.
This is called Multiple Inheritance.
Graphics Shape
Circle
36
Uses of Multiple Inheritance
This is required when a class has to
inherit behavior from multiple
classes.
In the example Circle class can
inherit move() operation from the
Shape class and the paint() operation
from the Graphics class.
Multiple inheritance is not supported
in JAVA but is supported in C++.
37
Uses of Inheritance – Multiple
Inheritance
GraphicCircle Shape
color centre
area()
paint() circumference()
move(newCentre)
Circle
radius
area()
circumference()
38
Polymorphism
Polymorphic which means “many forms”
has Greek roots.
Poly – many
Morphos - forms.
40
41
Polymorphism – Method Overloading
44
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
45
Benefits of OOP..
Inheritance: Elimination of Redundant
Code and extend the use of existing
classes.
Build programs from existing working
modules, rather than having to start
from scratch. save development
time and get higher productivity.
Encapsulation: Helps in building
secure programs.
46
Benefits of OOP..
Multiple objects to coexist without
any interference.
Easy to map objects in problem
domain to those objects in the
program.
It is easy to partition the work in a
project based on objects.
The Data-Centered Design enables us
in capturing more details of model in
an implementable form.
47
Benefits of OOP..
49
References
50
51