Introduction To Object-Oriented Programming

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

OOP: Introduction

1
Introduction to Object-Oriented Programming

Objects and classes

Abstract Data Types (ADT)

ncapsulation and in!ormation "iding

Aggregation

In"eritance and polymorp"ism


OOP: Introduction
#
Pure Object-Oriented $anguages
%i&e rules '(ource: Alan )ay*:

&eryt"ing in an object+

A program is a set o! objects telling eac" ot"er ,"at to do by


sending messages+

ac" object "as its o,n memory (made up by ot"er objects)+

&ery object "as a type+

All objects o! a speci!ic type can recei&e t"e same messages+


-a&a brea.s some o! t"ese rules in t"e name o! e!!iciency+
OOP: Introduction
/
T"e Object 0oncept

An object is an encapsulation o! data+

An object "as

identity (a uni1ue re!erence)2

state2 also called c"aracteristics

be"a&ior

An object is an instance o! an abstract data type+

An abstract data type is implemented &ia a class+


OOP: Introduction
3
Abstract Data Type (ADT)

An ADT is a collection o! objects (or values) and a


corresponding set o! methods.

An ADT encapsulates t"e data representation and ma.es data


access possible at a "ig"er le&el o! abstraction+

4ample 1: A set o! &e"icles ,it" operations !or starting2


stopping2 dri&ing2 get .m5liter2 etc++

4ample #: A time inter&al2 start time2 end time2 duration2


o&erlapping inter&als2 etc+
OOP: Introduction
6
ncapsulation and In!ormation 7iding

Data can be encapsulated suc" t"at it is in&isible to t"e 8outside


,orld8+

Data can only be accessed &ia met"ods+


Data
%unction
%unction
%unction
Data
9et"od
9et"od
9et"od
ADT Procedural
send
message
OOP: Introduction
:
ncapsulation and In!ormation 7iding2 cont+

;"at t"e 8outside ,orld8 cannot see it cannot depend on<

T"e object is a 8!ire-,all8 bet,een t"e object and t"e 8outside


,orld8+

T"e "idden data and met"ods can be c"anged ,it"out a!!ecting


t"e 8outside ,orld8+
7idden data and met"ods
0lient inter!ace
=isible data and met"ods An object
OOP: Introduction
>
0lass &s+ Object
0lass

A description o! t"e
common properties o! a
set o! objects+

A concept+

A class is a part o! a
program+

4ample 1: Person

4ample #: Album
Object

A representation o! t"e
properties o! a single
instance+

A p"enomenon+

An object is part o! data


and a program e4ecution+

4ample 1: ?ill 0linton2


?ono2 =iggo -ensen+

4ample #: A 7ard Day@s


Aig"t2 -os"ua Tree2 Bic.ie
$ee -ones+
OOP: Introduction
C
Type and Inter!ace

An object "as type and an inter!ace+


Account
balance()
withdraw()
deposit()
Type
Inter!ace

To get an object Account a = new Account()

To send a message a.withdraw()


OOP: Introduction
D
Instantiating 0lasses

An instantiation is a mec"anism ,"ere objects are created !rom


a class+

Al,ays in&ol&es storage allocation !or t"e object+

A mec"anism ,"ere objects are gi&en an initial state+


Static Instantiating

In t"e declaration part o! a


program+

A static instance is implicitly


created
Dynamic Instantiating

In t"e met"od part o! a


program+

A dynamic instance is
created e4plicitly ,it" a
special command+
OOP: Introduction
1E
Interaction bet,een Objects

Interaction bet,een objects "appens by messages being send+

A message acti&ates a met"od on t"e calling object+

An object O1 interacts ,it" anot"er object O# by calling a


met"od on O# (must be part o! t"e client inter!ace)+

FO1 sends O# a messageG

O1 and O# must be related to communicate+

T"e call o! a met"od corresponds to a procedure call in a non-


object-oriented language suc" as 0 or Pascal+
O1
O#
O/
message
message
message
OOP: Introduction
11
P"enomenon and 0oncept

A phenomenon is a t"ing in t"e FrealG ,orld t"at "as indi&idual


e4istence+

A concept is a generaliHation2 deri&ed !rom a set o! p"enomena


and based on t"e common properties o! t"ese p"enomena+

0"aracteristics o! a concept

A name

Intension2 t"e set o! properties o! t"e p"enomenon

Extension2 t"e set o! p"enomena co&ered by t"e concept+


OOP: Introduction
1#
0lassi!ication and 4empli!ication

A classification is a description o! ,"ic" p"enomena t"at


belongs to a concept+

An exemplification is a p"enomenon t"at co&ers t"e concept


0oncept
P"enomenon
classi!ication e4empli!ication
OOP: Introduction
1/
Aggregation and Decomposition

An aggregation consists o! a number o! (sub-)concepts ,"ic"


collecti&ely is considered a ne, concept+

A decomposition splits a single concept into a number o!


(sub-)concepts+
0oncept
0oncept 0oncept 0oncept
decomposition
0oncept 0oncept 0oncept
0oncept
aggregation
OOP: Introduction
13
Aggregation and Decomposition2 4ample

Idea: ma.e ne, objects by combining e4isting objects+

Reusing the implementation!


Car
start()
drive()
Engine
Gearbox
Doors[4
Engine
start()
stop()
Gearbox
up()
down()
Door
open()
close()
ne, class
e4isting classes
OOP: Introduction
16
IeneraliHation and (pecialiHation

Generaliation creates a concept ,it" a broader scope+

Specialiation creates a concept ,it" a narro,er scope+

Reusing the interface!


0oncept A
0oncept ?
specialiation
0oncept 0
0oncept D
generaliation
=e"icle
0ar Truc.
7atc"bac. (tation car (edan
Pic.up
OOP: Introduction
1:
IeneraliHation and (pecialiHation2 4ample

Inheritance: get t"e inter!ace !rom t"e general class+

Objects related by in"eritance are all o! t"e same type+


("ape
draw()
resi!e()
0ircle
draw()
resi!e()
$ine
draw()
resi!e()
Bectangle
draw()
resi!e()
(1uare
draw()
resi!e()
OOP: Introduction
1>
0ode 4ample

!olymorphism: One piece o! code ,or.s ,it" all s"ape objects+

Dynamic binding: 7o, polymorp"ism is implemented+


void do"o#ething("hape s)$
s.draw()% && '#agicall() calls on speci*ic class
s.resi!e()%
+
Circle c = new Circle()%
,ine l = new ,ine()%
-ectangle r = new -ectangle()%
do"o#ething(c)% && d(na#ic binding
do"o#ething(l)%
do"o#ething(r)%
OOP: Introduction
1C
(tructuring by Program or DataJ

;"at are t"e actions o! t"e program &s+ ,"ic" data does t"e
program act on+

"op#do$n% (tep,ise program re!inement

&ottom#up: %ocus on t"e stable data parts t"en add met"ods

Object-oriented programming is bottom-up+ Programs are


structure ,it" outset in t"e data+

0 and Pascal programs are typically implemented in a more top-do,n


!as"ion+
OOP: Introduction
1D
-a&a Program (tructure
met"od body
met"od "eader
&& co##ent on the class
public class .(/rog $
"tring s = )0iggo)%
&11
1 2he #ain #ethod (co##ent on #ethod)
1&
public static void #ain ("tring[ args)$
&& 3ust write so#e stu**
"(ste#.out.println (45ello 6orld4)% +
+
&ariable
OOP: Introduction
#E
-a&a 0lass 4ample Car
&11 A si#ple class #odeling a car. 1&
public class Car $
&& instance variables
private "tring #a7e% private "tring #odel%
private double price%
&& "tring representation o* the car
public Car("tring #8 "tring #o8 double p) $
#a7e = #% #odel = #o% price = p%
+
&& "tring representation o* the car
public "tring to"tring() $
return 4#a7e9 4 : #a7e : 4 #odel9 4
: #odel : 4 price9 4 : price%
+
+
OOP: Introduction
#1
?yte 0ode &s+ 4ecutable
.(/rog.3ava
-a&a =irtual 9ac"ine
Operating (ystem
-a&a 0lass %ile
.(/rog.class
Portable ?yte 0ode
.(/rog.cpp
Operating (ystem
4ecutable myprog+e4e
3avac .(/rog.3ava
gcc .(/rog.cpp
;o #(prog.exe
OOP: Introduction
##
7istory o! -a&a

1DDE Oa. (interacti&e tele&ision2 big !ailure)

1DD3 -a&a (!or t"e Internet)

9ain !eature: 8;rite Once2 Bun Any ;"ere8


KL ,rap t"e operating system so t"ey all loo. t"e same
Designed !or

A !res" start (no bac.,ard compatibility)

8Pure8 OOP: 0MM (ynta42 (malltal. style

Impro&ements o&er 0MM muc" "arder to ,rite a bad program

Internet programming

=ery "ard to create a &irus

Bun in a ,eb bro,ser (and at t"e ser&er)

T"ere is a speed issue (!rom -a&a 1+/ and up muc" better)


OOP: Introduction
#/
Di!!erence !rom 050MM

&eryt"ing resides in a class

&ariables and met"ods

Iarbage collection

rror and e4ception "andling "andling

Ao global &ariables or met"ods

Ao local static &ariables

Ao separation o! declaration and implementation (no "eader


!iles)+

Ao e4plicit pointer operations (uses re!erences)

Ao preprocessor (but somet"ing similar)

7as !e,er 8dar. corners8

7as a muc" larger standard library


OOP: Introduction
#3
(ummary

0lasses are 8recipes8 !or creating objects

All objects are instances o! classes

An ADT is implemented in a class

Aggregation and decomposition

F"as-aG relations"ip

IeneraliHation and specialiHation

Fis-aG or Fis-li.e-aG relations"ip

ncapsulation

)ey !eature o! object-oriented programming

(eparation o! inter!ace !rom implementation

It is not possible to access t"e pri&ate parts o! an object

You might also like