Upload
srinu1221
View
217
Download
0
Embed Size (px)
Citation preview
8/8/2019 OOP Orientation 1
1/31
INTERNAL
March 23, 2009
OOP Orientation 1
Object Oriented Programming Concepts
8/8/2019 OOP Orientation 1
2/31
OOP Orientation
INTERNAL- 1 -
Course Objective Object Oriented Programming Concepts
Basic Concepts
(Introduction to classes, objects ,messaging, class
relationships)
Advanced Concepts
(Concepts like Inheritance, Polymorphism, Encapsulation ..)
8/8/2019 OOP Orientation 1
3/31
OOP Orientation
INTERNAL- 2 -
Overview
Introduction
Object Oriented Paradigm
Keys of OO Technology
Objects
Characteristics of Objects
Class
Message Passing
Classes Vs. Objects
Advantages of OOP
8/8/2019 OOP Orientation 1
4/31
OOP Orientation
INTERNAL- 3 -
Software A Complex World!
~ 8,000 new or modified SLOC*/developer/year (2005)
~ 800,000,000,000 cumulative SLOC (1950-2005)
* SLOC : Source Lines of Code
This complexity has led to many problems with large software projects ,termed
as Software Crisis
The software crisis manifests itself in
cost overrunsuser dissatisfaction with the final product
buggy software
brittle software
8/8/2019 OOP Orientation 1
5/31
OOP Orientation
INTERNAL- 4 -
How to deal with complexity
How do humans cope with complexity in everyday life?
Abstraction
Humans deal with complexity by abstracting details away.
Examples:
Driving a car doesnt require knowledge of internal combustion engine; sufficient to
think of a car as simple transport.
8/8/2019 OOP Orientation 1
6/31
OOP Orientation
INTERNAL- 5 -
OO Object Orientation
In OO
the main focus is to form abstractions called objects
Well put our abstractions into a hierarchy to keep them organized
natural way to divide and conquer (OO Decomposition)
our implementation decisions are easier to postpone since they arent visible.
fidelity of the abstraction to the real world. (Computing as Simulation)
First OO Language - Simula
Best current means of dealing with complexity.
OO has strong historical roots in other paradigms and practices. It came about
to address the software crisis.
8/8/2019 OOP Orientation 1
7/31
OOP OrientationINTERNAL
- 6 -
oving from a procedure-oriented to an object-oriented mindset
Modules are nested into a calling tree:
The topology of a structure program is inherently different than the topology of an OO program.
Objects cluster together by those which
communicate most frequently.Less ordered.
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.
8/8/2019 OOP Orientation 1
8/31
OOP OrientationINTERNAL
- 7 -
Physical Entity
White Tata Truck
An object is an entity (tangible or intangible) that has well defined structure and behavior
Examples of Objects :
Tata Truck
Black Parker Pen
Dr.John
Red Color
Point(3,0)
H+ + OH- > H2O (neutralization)
Linked List
What is an Object??
Conceptual Entity
Chemical Process Linked List
In OOP, a program is seen as comprising of a collection ofobjects, that act on
each other.
Each object having a distinct role or responsibility.
8/8/2019 OOP Orientation 1
9/31
OOP OrientationINTERNAL
- 8 -
The responsibility of an object is the role it serves within the system
Consider the object Ishanths Blue Alto
Every Object has a distinct Role or Responsibility
Responsibility of this Alto could be :To carry Ishanth from One place to Another
8/8/2019 OOP Orientation 1
10/31
OOP OrientationINTERNAL
- 9 -
How it achieves this responsibility ?
Each part of the car having its own
responsibility
.
8/8/2019 OOP Orientation 1
11/31
OOP OrientationINTERNAL
- 1 0 -
Responsibilities 2 Types
Car Operations
Accelerate
Apply Breaks
Change Gear
.
8/8/2019 OOP Orientation 1
12/31
OOP OrientationINTERNAL
- 1 1 -
Characteristics of Objects
Object has following characteristics
Responsibility
State
Behavior
Identity
8/8/2019 OOP Orientation 1
13/31
OOP OrientationINTERNAL
- 1 2 -
State What an object knows
It is a set of properties which can take different values at different times in the objects life.
Model = LXI
Fuel type = Petrol
Speed = 45 km/hr
Fuel level = Full
Tyre pressure = 30Gear Position = 4
Constant/Static
Dynamic
Lets have a look into The Blue Altos properties
Knowing Responsibilty ~ State
8/8/2019 OOP Orientation 1
14/31
OOP OrientationINTERNAL
- 1 3 -
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.
Doing Responsibilty ~ Behavior
8/8/2019 OOP Orientation 1
15/31
OOP OrientationINTERNAL
- 1 4 -
Identity
Identity is that property of an object which distinguishes it from all other objects
IDENTITY
Reg. No. uniquely identifies a car among all others
Car
Reg. No
Color
MakeFuel type
Speed
Fuel level
Tyre pressure
Gear
8/8/2019 OOP Orientation 1
16/31
OOP OrientationINTERNAL
- 1 5 -
How you define the Software Objects
Class Globe { }
A Class defines the capabilities or behavior of an object
8/8/2019 OOP Orientation 1
17/31
OOP OrientationINTERNAL
- 1 6 -
Class
A class characterizes a set of objects that share a common structure and a common behavior
A class is used as a template for the creation of similar objects
An object is said to be an instance of a class
A class is the blueprint from which individual objects are created.
The attributes/ Fields defined in a class defines the state of an object
The methods of the class defines the object behavior.
8/8/2019 OOP Orientation 1
18/31
OOP OrientationINTERNAL
- 1 7 -
Class Vs. Object
Class Object
Class is a type/template for similar
objects
Object is an instance of the class, with
each instance behaving identically
Class is purely a static concept,
represented by program text
Object is dynamic/run-time entity,
occupying space in memory
8/8/2019 OOP Orientation 1
19/31
OOP OrientationINTERNAL
- 1 8 -
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))) ;
}
}
8/8/2019 OOP Orientation 1
20/31
OOP OrientationINTERNAL
- 1 9 -
How to identify objects & their behaviors???
The nouns in the requirements specification would suggest the Objects in
the problem domain.
The verbs from the requirements specification would correspond to object
behaviors.
8/8/2019 OOP Orientation 1
21/31
OOP OrientationINTERNAL
- 2 0 -
Consider the following scenario:
Mr. White is married. He teaches OO Software Engineering
classes on Fridays. He is a part-time member of the faculty at the
CS Department of the All-Smart Institute. John is enrolled in the
OOSE class that Mr. White teaches. Mrs. White uses a Nano for
transportation to and from the campus (she teaches Philosophy at
the same institute). Class size is limited at the institute to 14
students. Janet, the sister of John is enrolled for violin course in
the same institute.
All Smart Group Activity
8/8/2019 OOP Orientation 1
22/31
OOP OrientationINTERNAL
- 2 1 -
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
8/8/2019 OOP Orientation 1
23/31
OOP OrientationINTERNAL
- 2 2 -
Demo All Smart Institute
Classes
Objects
8/8/2019 OOP Orientation 1
24/31
OOP OrientationINTERNAL
- 2 3 -
Constructors To create objects with proper state
Used to create customized objects of a class.
Customization means, we can initialize the fields/attributes during the creation time. The name of the constructor and class should be same.
8/8/2019 OOP Orientation 1
25/31
OOP OrientationINTERNAL
- 2 4 -
Mr. White When is
your OOSE
session?
Analyze the message sent by John to Mr. White
Identity of the recipient object - Mr. White
Behavior to be invoked by the recipient - Tell when the session isExtra information Required to invoke the behavior OOSE Name of
the session
Now Mr.White responds correctly.
Friday Morning9.00 am
Object Interaction Real World
8/8/2019 OOP Orientation 1
26/31
OOP OrientationINTERNAL
- 2 5 -
Object Interaction Software World
As in real world, Software Objects also interact by passingmessages.
In Software World, messages are dispatched to Methods :
which defines the behavior of the object for a message
Code to be executed by an object (to invoke a behavior of an
object), when a message is sent to it is known as a Method.
Message passing is achieved by invoking the methods of
the objects.
8/8/2019 OOP Orientation 1
27/31
OOP OrientationINTERNAL
- 2 6 -
Types of methods of a class
Constructors (We already discussed)
Accesors /Getters (Accesses, but does not change the state )
Mutators /Setters (Changes the state of the object )
Methods that defines other behaviors
>> Demo
8/8/2019 OOP Orientation 1
28/31
OOP OrientationINTERNAL
- 2 7 -
Why OOP ?
Square
rotate(){
//code
}
playSound(){
//code
}
Triangle
rotate(){
//code
}
playSound(){
//code
}
The spec
Jack proceduralprogrammer
Maria OOprogrammer
Here is my code with rotate & playSound as theprocedures
rotate(shapenum){
//make the shape rotate 360
}
playSound(shapenum){
//use shapenum to find which AIF to play
}
Here's the code for the classes:
Circle
rotate(){
//code
}
playSound(){
//code
}
Well! Let me identifythe procedures
I need. After all what is
a program if not alist of procedures.
Well! Let me identifythe key players.
They can be theclasses of my code
8/8/2019 OOP Orientation 1
29/31
OOP OrientationINTERNAL
- 2 8 -
OOPS!!! The spec has changedWho is more affected???
The modification in the spec
Hmm!! The playSound codehas to be changedwhich
means I will have to rewrite
the code every time thespec changes
playSound(shapenum){
//if the shape is not amoeba, use shapenum to
//find which AIF to play else
//play amoeba.hif sound
}
Cool!! I just have towrite one more class &I dont have to touch
the code alreadywritten.
Amoeba
rotate(){
//code}
playSound(){
//code to play .hif file
}
Oh Great!! Howefficient OOP is with so
manyfeatures.Flexibility,
extensibility.
8/8/2019 OOP Orientation 1
30/31
OOP OrientationINTERNAL
- 2 9 -
Session 2,3 & 4
Session 2 : Model Objects from Problem Domain
Session 3 : First Feel of Objects using Blue J
Session 4 : Message Passing / Mutators / Accessors
8/8/2019 OOP Orientation 1
31/31
Happy Learning !