CH 1 Introduction To OOP Paradigm
CH 1 Introduction To OOP Paradigm
NJ NY NJ NY
Hudson river Hudson river
Object-Oriented Programming: OOP
• A design and programming technique
• Some terminology:
– object - usually a person, place or thing (a noun)
– method - an action performed by an object (a verb)
– type or class - a category of similar objects (such as
automobiles)
• Objects have both data and methods
• Objects of the same class have the same data
elements and methods
• Objects send and receive messages to invoke
actions
Object-Oriented Programming - Example
(Object Technology - A
Manager’s Guide, Taylor,
1997)
The History of Object Technology
• Major object technology milestones
– Physical entity
Truck
– Conceptual entity
Chemical
Process
– Software entity
Linked List
A More Formal Definition
• An object is an entity with Attributes
a well-defined boundary
and identity that
encapsulates state and
behavior.
– State is represented by
attributes and relationships.
– Behavior is represented by
operations, methods, and
Object
state machines.
Operations
An Object Has State
• The state of an object is one of the possible
conditions in which an object may exist.
• The state of an object normally changes over
time.
Name: J Clark
Employee ID: 567138
HireDate: 07/25/1991
Professor Clark Status: Tenured
Discipline: Finance
MaxLoad: 3
Name: J Clark
Employee ID: 567138
Date Hired: July 25, 1991
Status: Tenured
Discipline: Finance
Maximum Course Load: 3 classes Professor Clark
An Object Has Behavior
• Behavior determines how an object acts and reacts.
• The visible behavior of an object is modeled by the
set of messages it can respond to (operations the
object can perform).
Ac
ce
ptC
ou
()
r se
s
de
Of
ra
fer
alG
ing
in
( )
itF
bm
Su
Professor Clark
Se
tM
ax
Lo
a d(
Professor Clark’s behavior )
Class: Professor
Professor
- name
Professor Jones
Operations
A Sample Class
Class: Automobile
Data Items: Methods:
– manufacturer’s name – Define data items
– model name (specify
– year made manufacturer’s name,
model, year, etc.)
– color
– Change a data item
– number of doors
(color, engine, etc.)
– size of engine
– Display data items
– etc.
– Calculate cost
– etc.
The Relationship Between Classes and
Objects
• A class is an abstract definition of an object.
– It defines the structure and behavior of each object in the class.
– It serves as a template for creating objects
• Objects are grouped into classes.
• An object is an instance of a class.
From Real World Class: Professor
Professor Jones Professor Smith abstracting Professor
Objects - name
- employeeID : UniqueId
- hireDate
- status
- discipline
Professor Mellon - maxLoad
instancing + submitFinalGrade()
J Clark : Objects + acceptCourseOffering()
+ setMaxLoad()
Professor
To computer World + takeSabbatical()
What Is an Attribute?
• An attribute is a named property of a class that
describes a range of values instances of the
property may hold.
– A class may have any number of attributes or no attributes at
all.
Student
- name
- address
Attributes
- studentID
- dateOfBirth
Attributes in Classes and Objects
Student Objects
- name
- address
- studentID
- dateOfBirth
name: D. Hatcher
address: 456 Oak
studentID: 2
dateofBirth: 12/11/1969
What Is an Operation?
• An operation is the implementation of a service
that can be requested from any object of the
class to affect behavior.
• A class may have any number of operations or
none at all.
Student
+ get tuition()
+ add schedule()
Operations + get schedule()
+ delete schedule()
+ has pre-requisites()
Example: class Professor
class Professor {
private String name;
private int age; Professor
private String speciality;
- name : String
public Professor (String sm, int ia,
- age : int
String ss) {
name = sm; - speciality : String
age = ia;
speciality = sst; +getName() : String
} +getAge() : int
public String getName () { return +getSpeciality() : String
name;}
public int getAge () { return age;}
public String getSpeciality () {
return speciality;}
}
Example : Instance of Professor
wang : Professor
name = “wang”
age = 35
speciality = “computer”
Professor wang
wang.getName()
Example: Object Interaction
• The OrderEntryForm wants Order to calculate the
total dollar value for the order.
()
ta l
rTo
e
calculateOrderTotal()
rd
te O
la
lcu
orderID
Ca
date
salesTotal
tax
shipDate
Message
OrderEntryForm Order
The class Order has the responsibility to calculate the total dollar value.
Basic Principles of Object Orientation
Object Orientation
Polymorphism
Encapsulation
Inheritance
Abstraction
What Is Abstraction?
Abstraction can be defined as:
Any model that includes the most important, essential, or
distinguishing aspects of something while suppressing or
ignoring less important, immaterial, or diversionary details.
The result of removing distinctions so as to emphasize
commonalties.
(Dictionary of Object Technology, Firesmith, Eykholt, 1995)
Abstraction
Emphasizes relevant characteristics.
Suppresses other characteristics.
BriefCase
- Capacity
- Weight
+ open()
+ close()
Example: Abstraction
Student Professor
An analogy:
• When you drive a car, you don’t have know the details
of how many cylinders the engine has or how the
gasoline and air are mixed and ignited.
• Instead you only have to know how to use the
controls.
What Is Encapsulation?
Hide implemmentation from clients
clients depend on interface
Improves Resiliency
Encapsulation Illustrated
Professor Clark
• Professor Clark
needs to be able to Ac
ce
ptC
teach four classes in
s ()
ou
de
rs e
ra
Of
lG
the next semester. fer
ina
ing
itF
Name: J Clark
bm
Employee ID: 567138 ()
Su
HireDate: 07/25/1991
Status: Tenured
Discipline: Finance
Se MaxLoad:4
SetMaxLoad(4) tM
ax
Lo
ad
( )
TakeSabbatical()
Encapsulation –
Information/Implementation hiding
Information which can’t be
accessed by client
Balance
Interface insterestYTD
Owner
Client Deposit() Account_number
Withdraw()
Transfer() Deposit() {…}
Withdraw() {…}
Transfer() {…}
What properties does each vehicle inherit from the types of vehicles
above it in the diagram?
Example: Single Inheritance
• One class inherits from another.
Ancestor
Account
- balance
Superclass - name
- number
(parent)
+ withdraw()
+ createStatement()
Inheritance
Relationship
Savings Checking
Subclasses
Descendents
Example: Multiple Inheritance
• A class can inherit from several other classes.
FlyingThing Animal
Multiple Inheritance
Manufacturer B
Manufacturer A Manufacturer C
OO Principle:
Encapsulation
Example: Polymorphism
Get Current Value
get get
Cu Cu get
rr e rr e Cu
ntV ntV rr e
alu alu ntV
e( ) e( ) alu
e( )
‘What’ Pyramid
draw()
move()
scale()
rotate() Cube
Shape
Cube
Tube
Canonical
<<Interface>>
(Class/Stereotype) Shape
Representation Pyramid
draw()
move()
scale()
rotate() Cube
Circle Rectangle
draw () draw ()