0% found this document useful (0 votes)
49 views30 pages

OOP Orientation 1

The document provides an introduction to object-oriented programming concepts. It discusses basic concepts like classes, objects, messaging and class relationships. It also covers advanced concepts such as inheritance, polymorphism and encapsulation. The document explains that OOP addresses the "software crisis" by using objects and abstraction to deal with complexity. It defines key OOP concepts such as objects, classes, states, behaviors, identities and the relationships between classes and objects.

Uploaded by

srinu1221
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views30 pages

OOP Orientation 1

The document provides an introduction to object-oriented programming concepts. It discusses basic concepts like classes, objects, messaging and class relationships. It also covers advanced concepts such as inheritance, polymorphism and encapsulation. The document explains that OOP addresses the "software crisis" by using objects and abstraction to deal with complexity. It defines key OOP concepts such as objects, classes, states, behaviors, identities and the relationships between classes and objects.

Uploaded by

srinu1221
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

OOP – Orientation 1

Object Oriented Programming Concepts

INTERNAL

March 23, 2009


Course Objective – Object Oriented Programming Concepts

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!

• ~ 8,000 new or modified SLOC*/developer/year (2005)


• ~ 800,000,000,000 cumulative SLOC (1950-2005)

This complexity has led to many problems with large software projects ,termed
as Software Crisis

The “software crisis” manifests itself in


•cost overruns
•user dissatisfaction with the final product
•buggy software
•brittle software

* SLOC : Source Lines of Code


OOP Orientation
-4-
INTERNAL
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 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

– the main focus is to form abstractions called objects

– We’ll 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 aren’t visible.

– fidelity of the abstraction to the real world. (Computing as Simulation)


• First OO Language - Simula

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.

•Objects cluster together by those which


•Modules are nested into a calling tree:
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.

OOP Orientation
-7-
INTERNAL
What is an Object??
An object is an entity (tangible or intangible) that has well defined structure and behavior

Examples of Objects : Physical Entity


• Tata Truck
• Black Parker Pen
• Dr.John
• Red Color White Tata Truck
• Point(3,0)
• H+ + OH- > H2O (neutralization) Conceptual Entity
• Linked List

Chemical Process Linked List

• In OOP, a program is seen as comprising of a collection of objects, that act on


each other.
• Each object having a distinct role or responsibility.

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

Consider the object “Ishanth’s Blue Alto”

Responsibility of this Alto could be :


To carry Ishanth from One place to Another

OOP Orientation
-9-
INTERNAL
How it achieves this responsibility ?

….

Each part of the car having it’s own


responsibility
OOP Orientation
- 10 -
INTERNAL
Responsibilities – 2 Types

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.

Let’s have a look into “The Blue Alto’s properties”


Model = LXI
Fuel type = Petrol Constant/Static

Speed = 45 km/hr
Fuel level = Full
Tyre pressure = 30 Dynamic
Gear Position = 4

Knowing Responsibilty ~ State

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.

Doing Responsibilty ~ Behavior

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

Reg. No. uniquely identifies a car among all others

OOP Orientation
- 15 -
INTERNAL
How you define the Software Objects

Class Globe { … }

A Class defines the capabilities or behavior of an object

OOP Orientation
- 16 -
INTERNAL
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.

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

Class is purely a static concept, Object is dynamic/run-time entity,


represented by program text occupying space in memory

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???

• 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.

OOP Orientation
- 20 -
INTERNAL
All Smart Group Activity

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.

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

• 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.

OOP Orientation
- 24 -
INTERNAL
Object Interaction – Real World

Friday Morning
9.00 am
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 is
•Extra information Required to invoke the behavior – “OOSE – Name of
the session”

Now Mr.White responds correctly.

OOP Orientation
- 25 -
INTERNAL
Object Interaction – Software World

• As in real world, Software Objects also interact by passing


messages.

• 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.

OOP Orientation
- 26 -
INTERNAL
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

OOP Orientation
- 27 -
INTERNAL
Why OOP ?…

Well! Let me identify


the procedures Well! Let me identify
I need. After all what is the key players.
a program if not a They can be the
list of procedures. classes of my code

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

The modification in the spec

playSound(shapenum){ Amoeba

//if the shape is not amoeba, use shapenum to rotate(){


//code
Oh Great!! How
//find which AIF to play else }
efficient OOP is with so
many playSound(){
//play amoeba.hif sound features….Flexibility, //code to play .hif file
extensibility….
} }

OOP Orientation
- 29 -
INTERNAL
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

OOP Orientation
- 30 -
INTERNAL
Happy Learning !

You might also like