Anda di halaman 1dari 17

1

2
*ldenufylng Lhe sLaLe and behavlor for real-world ob[ecLs ls a greaL way Lo begln
Lhlnklng ln Lerms of ob[ecL-orlenLed programmlng.
*uogs have sLaLe (name, color, breed, hungry) and behavlor (barklng, feLchlng,
wagglng Lall)
*8lcycles also have sLaLe (currenL gear, currenL pedal cadence, currenL speed) and
behavlor (changlng gear, changlng pedal cadence, applylng brakes)
*lor each ob[ecL LhaL you see, ask yourself Lwo quesuons:
3
*Soware ob[ecLs are concepLually slmllar Lo real-world ob[ecLs: Lhey Loo conslsL of
sLaLe and relaLed behavlor.

4
8y aurlbuung sLaLe (currenL speed, currenL pedal cadence, and currenL gear) and
provldlng meLhods for changlng LhaL sLaLe, Lhe ob[ecL remalns ln conLrol of how Lhe
ouLslde world ls allowed Lo use lL. lor example, lf Lhe blcycle only has 6 gears, a
meLhod Lo change gears could re[ecL any value LhaL ls less Lhan 1 or greaLer Lhan 6.
3
1here may be Lhousands of oLher blcycles ln exlsLence, all of Lhe same make and
model. Lach blcycle was bullL from Lhe same seL of blueprlnLs and Lherefore conLalns
Lhe same componenLs. ln ob[ecL-orlenLed Lerms, we say LhaL your blcycle ls
an !"#$%"&' of Lhe &(%## *+ *,-'&$# known as blcycles.

?ou may have nouced LhaL Lhe 8lcycle class does noL conLaln a maln meLhod. 1haL's
because lL's noL a compleLe appllcauon, lL's [usL Lhe blueprlnL for blcycles LhaL mlghL
be .#'/ ln an appllcauon. 1he responslblllLy of creaung and uslng new 8lcycle ob[ecLs
belongs Lo some oLher class ln your appllcauon.
6
7
1he ouLpuL of Lhls LesL prlnLs Lhe endlng pedal cadence, speed, and gear for Lhe Lwo
blcycles:
cadence:30 speed:10 gear:2
cadence:40 speed:20 gear:3

8
9
10
ln ob[ecL-orlenLed programmlng, absLracuon means lgnorlng Lhe non-essenual deLalls
of an ob[ecL and concenLraung on lLs essenual deLalls.

All Lhe aurlbuLes of Lhe ob[ecLs of Lhe classes are dened ln Lhe class. Powever, you
cannoL sLore any daLa ln a class because creaung a class does noL allocaLe any
memory space Lo Lhe class. 1o sLore daLa, you need Lo creaLe ob[ecLs of Lhe class,
whlch have memory allocaLed as soon as lL ls creaLed. Classes form Lhe LemplaLes for
creaung ob[ecLs.

Slmllarly, when you wanL Lo send e-mall messages, you should know Lhe processes of
wrlung e-mall messages and sendlng lL Lo Lhe recelver. Powever, lL ls noL necessary
for you Lo know Lhe enure process of sendlng Lhe e-mall messages across Lhe
neLwork.

daLa absLracuon ls noLhlng more Lhan Lhe lmplemenLauon of
an ob[ecL LhaL conLalns Lhe same essenual properues and acuons we
can nd ln Lhe orlglnal ob[ecL we are represenung.

AbsLracuon refers Lo showlng only Lhe necessary deLalls Lo Lhe lnLended user. As Lhe
11
Cen, encapsulauon ls referred Lo as "lnformauon hldlng." An ob[ecL
"hldes" Lhe lmplemenLauon of lLs behavlor behlnd lLs lnLerface or lLs "publlc
face." CLher ob[ecLs can use lLs behavlor wlLhouL havlng deLalled knowledge
of lLs lmplemenLauon. Cb[ecLs know only Lhe klnd of operauons Lhey can
requesL oLher ob[ecLs Lo perform. 1hls allows soware deslgners Lo absLracL
from lrrelevanL deLalls and concenLraLe on whaL ob[ecLs wlll perform.

Lncapsulauon ls Lhe feaLure LhaL provldes securlLy Lo Lhe daLa and Lhe meLhods of a
class.

LncapsulaLe means Lo hlde. Lncapsulauon ls also called daLa hldlng.?ou can Lhlnk
Lncapsulauon llke a capsule (medlclne LableL) whlch hldes medlclne lnslde lL.
Lncapsulauon ls wrapplng, [usL hldlng properues and meLhods. Lncapsulauon ls used
for hlde Lhe code and daLa ln a slngle unlL Lo proLecL Lhe daLa from Lhe ouLslde Lhe
world. Class ls Lhe besL example of encapsulauon.

12
Cen, encapsulauon ls referred Lo as "lnformauon hldlng." An ob[ecL
"hldes" Lhe lmplemenLauon of lLs behavlor behlnd lLs lnLerface or lLs "publlc
face." CLher ob[ecLs can use lLs behavlor wlLhouL havlng deLalled knowledge
of lLs lmplemenLauon. Cb[ecLs know only Lhe klnd of operauons Lhey can
requesL oLher ob[ecLs Lo perform. 1hls allows soware deslgners Lo absLracL
from lrrelevanL deLalls and concenLraLe on whaL ob[ecLs wlll perform.

Lncapsulauon ls Lhe feaLure LhaL provldes securlLy Lo Lhe daLa and Lhe meLhods of a
class.
13
ulerenL klnds of ob[ecLs oen have a cerLaln amounL ln common wlLh each oLher.
MounLaln blkes, road blkes, and Landem blkes, for example, all share Lhe
characLerlsucs of blcycles (currenL speed, currenL pedal cadence, currenL gear). ?eL
each also denes addluonal feaLures LhaL make Lhem dlerenL: Landem blcycles have
Lwo seaLs and Lwo seLs of handlebars, road blkes have drop handlebars, some
mounLaln blkes have an addluonal chaln rlng, glvlng Lhem a lower gear rauo.

lnherlLance enables you Lo exLend Lhe funcuonallLy of an exlsung class.

When a class acqulre Lhe properLy of anoLher class ls known as lnherlLance.
lnherlLance ls process of ob[ecL reusablllLy.
lor example, A Chlld acqulre properLy of arenLs.





14
olymorphlsm ls derlved from Lwo Laun words-oly, whlch means many, and morph,
whlch means forms.

olymorphlsm means !"# "%&# &%"' (!)&*.
Cne funcuon behaves dlerenL forms.
ln oLher words, "Many forms of a slngle ob[ecL ls called olymorphlsm."





13
8y aurlbuung sLaLe (currenL speed, currenL pedal cadence, and currenL gear) and
provldlng meLhods for changlng LhaL sLaLe, Lhe ob[ecL remalns ln conLrol of how Lhe
ouLslde world ls allowed Lo use lL. lor example, lf Lhe blcycle only has 6 gears, a
meLhod Lo change gears could re[ecL any value LhaL ls less Lhan 1 or greaLer Lhan 6.

ModularlLy: 1he source code for an ob[ecL can be wrluen and malnLalned
lndependenLly of Lhe source code for oLher ob[ecLs. Cnce creaLed, an ob[ecL can be
easlly passed around lnslde Lhe sysLem.

lnformauon-hldlng: 8y lnLeracung only wlLh an ob[ecL's meLhods, Lhe deLalls of lLs
lnLernal lmplemenLauon remaln hldden from Lhe ouLslde world.

Code re-use: lf an ob[ecL already exlsLs (perhaps wrluen by anoLher soware
developer), you can use LhaL ob[ecL ln your program. 1hls allows speclallsLs Lo
lmplemenL/LesL/debug complex, Lask-speclc ob[ecLs, whlch you can Lhen LrusL Lo
run ln your own code.

luggablllLy and debugglng ease: lf a parucular ob[ecL Lurns ouL Lo be problemauc,
you can slmply remove lL from your appllcauon and plug ln a dlerenL ob[ecL as lLs
16
17

Anda mungkin juga menyukai