OOP Orientation 1
OOP Orientation 1
INTERNAL
Basic Concepts
(Introduction to classes, objects ,messaging, class
relationships)
Advanced Concepts
(Concepts like Inheritance, Polymorphism, Encapsulation ..)
OOP Orientation
-2-
INTERNAL
Overview
– Introduction
– Object Oriented Paradigm
– Keys of OO Technology
• Objects
• Characteristics of Objects
• Class
• Message Passing
• Classes Vs. Objects
• Advantages of OOP
OOP Orientation
-3-
INTERNAL
Software – A Complex World!
This complexity has led to many problems with large software projects ,termed
as Software Crisis
Abstraction
Humans deal with complexity by abstracting details away.
Examples:
Driving a car doesn’t require knowledge of internal combustion engine; sufficient to
think of a car as simple transport.
OOP Orientation
-5-
INTERNAL
OO – Object Orientation
OO has strong historical roots in other paradigms and practices. It came about
to address the “software crisis”.
Best current means of dealing with complexity.
• In OO
– our implementation decisions are easier to postpone since they aren’t visible.
OOP Orientation
-6-
INTERNAL
Moving from a procedure-oriented to an object-oriented mindset
The topology of a structure program is inherently different than the topology of an OO program.
The algorithms still exist but they are encapsulated into an object which represents the thing
in the problem domain most closely associated with that algorithm.
OOP Orientation
-7-
INTERNAL
What is an Object??
An object is an entity (tangible or intangible) that has well defined structure and behavior
OOP Orientation
-8-
INTERNAL
Every Object has a distinct Role or Responsibility
The responsibility of an object is the role it serves within the system
OOP Orientation
-9-
INTERNAL
How it achieves this responsibility ?
….
Car Operations
• Accelerate
• Apply Breaks
• Change Gear
• ….
i ng g
in
n ow Do
K
OOP Orientation
- 11 -
INTERNAL
State – What an object knows
It is a set of properties which can take different values at different times in the object’s life.
Speed = 45 km/hr
Fuel level = Full
Tyre pressure = 30 Dynamic
Gear Position = 4
OOP Orientation
- 13 -
INTERNAL
Behavior – What an object does
Behavior is how an object reacts, in terms of its state changes, in response to operations
performed upon it.
Car Operations
• Accelerate
• Apply Brake
• Change Gear
• ….
Totality of operations we can perform using a car and consequent changes in state
defines behavior of a car.
OOP Orientation
- 14 -
INTERNAL
Identity
Identity is that property of an object which distinguishes it from all other objects
Car
Reg. No
Color
Make
Fuel type
Speed IDENTITY
Fuel level
Tyre pressure
Gear
OOP Orientation
- 15 -
INTERNAL
How you define the Software Objects
Class Globe { … }
OOP Orientation
- 16 -
INTERNAL
Class
A class characterizes a set of objects that share a common structure and a common behavior
OOP Orientation
- 17 -
INTERNAL
Class Vs. Object
Class Object
Class is a type/template for similar Object is an instance of the class, with
objects each instance behaving identically
OOP Orientation
- 18 -
INTERNAL
Point Class – An Example
import java.math.*;
//Class Specification
//Responsibility: Represents a point with x & y coordinates, which can compute the distance from another point
class Point {
//Attributes defines the state: The coordinates of the point
double x;
double y;
//Methods define Behaviours of Objects
Point(double xCor, double yCor) { x = xCor; y = yCor;} // A constructor that initializes the fields
double getx() { return x; } //Getters
double gety() { return y; }
void setx(double xCor) { x = xCor; } //Setters
void sety(double yCor) { y = yCor; }
//Business Behaviors
//Contract: distance-from: Point->Number
//Purpose: Distance between P(x1,y1) & Q(x2,y2) = v (x2-x1)2 + (y2-y1)2
double distanceFrom(Point anotherPoint) {
return Math.sqrt(((anotherPoint.getx() - x)*(anotherPoint.getx() - x))
+ ((anotherPoint.gety() - y)*(anotherPoint.gety() - y))) ;
}
}
OOP Orientation
- 19 -
INTERNAL
How to identify objects & their behaviors???
OOP Orientation
- 20 -
INTERNAL
All Smart Group Activity
OOP Orientation
- 21 -
INTERNAL
From the scenario,
1. Identify all the objects
2. Identify their knowing/doing responsibilities
3. Identify the different states and behaviors of each object
4. Identify Similar Objects / Class or Type that defines
the objects
5. Identify attributes and methods of the class corresponding
to the state and behavior of objects identified
OOP Orientation
- 22 -
INTERNAL
Demo – All Smart Institute
Classes
Objects
OOP Orientation
- 23 -
INTERNAL
Constructors – To create objects with proper state
OOP Orientation
- 24 -
INTERNAL
Object Interaction – Real World
Friday Morning
9.00 am
Mr. White When is
your OOSE
session?
OOP Orientation
- 25 -
INTERNAL
Object Interaction – Software World
OOP Orientation
- 26 -
INTERNAL
Types of methods of a class
>> Demo
OOP Orientation
- 27 -
INTERNAL
Why OOP ?…
The spec
Maria – OO
Jack – procedural
programmer
programmer
Here's the code for the classes:
Here is my code with rotate & playSound as the
procedures Square
rotate(shapenum){ rotate(){ Circle
//make the shape rotate 360’ //code rotate(){ Triangle
} //code rotate(){
}
playSound(){ } //code
playSound(shapenum){ //code playSound(){ }
//use shapenum to find which AIF to play } //code playSound(){
} } //code
- 28 -
} OOP Orientation
INTERNAL
OOPS!!! The spec has changed…Who is more affected???
Cool!! I just have to
Hmm!! The playSound code write one more class &
has to be changed…which I don’t have to touch
means I will have to rewrite the code already
the code every time the written.
spec changes
playSound(shapenum){ Amoeba
OOP Orientation
- 29 -
INTERNAL
Session 2,3 & 4
OOP Orientation
- 30 -
INTERNAL
Happy Learning !