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

UNI

T1 I
ntr
oduct
iont
oOOP

I
ntr
oduct
iont
oObj
ectOr
ient
edPr
ogr
ammi
ng

 I
ntr
oduct
ion:
OneChar
act
eri
sti
cthati
scons
tanti
nthes
oft
war
eindus
tryt
oday
i
sthe “change”.Si
nce t
he i
nvent
ion oft
he comput
er,many pr
ogr
ammi
ng
appr
oaches have been t
ried. Thes
e i
ncl
ude t
echni
ques s
uch as modul
ar
pr
ogr
ammi
ng,t
op-
downpr
ogr
ammi
ng,bot
tom-
uppr
ogr
ammi
ngands
truct
ured
pr
ogr
ammi
ng.Thepr
imar
ymot
ivat
ioni
neachcas
ehasbeent
heconcer
ntohandl
e
t
hei
ncr
eas
ingcompl
exi
tyofpr
ogr
amst
hatar
erel
iabl
eandmai
ntai
nabl
e.

Wi
tht
headventofl
anguagess
uchasC,s
truct
uredpr
ogr
ammi
ngbecamever
y
popul
arandwast
hepar
adi
gm oft
he1980’
s.St
ruct
uredpr
ogr
ammi
ngbecamever
y
power
fult
oolt
hatenabl
edpr
ogr
ammer
stowr
itemoder
atel
ycompl
expr
ogr
ams
f
air
lyeas
y.However
,ast
hepr
ogr
amsgr
ew l
arger
,event
hes
truct
uredappr
oach
f
ail
ed t
oshow t
hedes
ired r
esul
tsi
nter
msofbug-
free,eas
y-t
o-mai
ntai
n,and
r
eus
abl
epr
ogr
ams
.

Obj
ect
-Or
ient
edPr
ogr
ammi
ng(
OOP)i
sanappr
oacht
opr
ogr
am or
gani
zat
ionand
devel
opment
,whi
chat
tempt
stoel
imi
nat
esomeoft
hepi
tf
all
sofconvent
ional
pr
ogr
ammi
ng met
hodsby i
ncor
por
ati
ng t
he bes
tofs
truct
ured pr
ogr
ammi
ng
f
eat
ureswi
ths
ever
alnewconcept
s.Javai
sapur
eobj
ect–or
ient
edl
anguage.

 NeedofOOP:
TwoPar
adi
gmsofPr
ogr
ammi
ng:
Allcomput
erpr
ogr
amscons
ist
oft
wo el
ement
s:codeand dat
a.Fur
ther
mor
e,apr
ogr
am can beconcept
ual
ly
or
gani
zedar
oundi
tscodeorar
oundi
tsdat
a.Thati
s,s
omepr
ogr
amsar
ewr
itt
en
ar
oundwhati
shappeni
ngandot
her
sar
ewr
itt
enar
oundwhoi
sbei
ngaf
fect
ed.
Thes
ear
ethet
wopar
adi
gmst
hatgover
nhowapr
ogr
am i
scons
truct
ed.

 Thef
irs
twayi
scal
ledt
hepr
oces
s-or
ient
edmodel
.Thi
sappr
oachchar
act
eri
zesa
pr
ogr
am asas
eri
esofl
inears
teps(
thati
s,code)
.Thepr
oces
s-or
ient
edmodel
canbet
houghtofascodeact
ingondat
a.Pr
ocedur
all
anguagess
uchasC
empl
oyt
hismodelt
ocons
ider
abl
esucces
s.Pr
obl
emswi
tht
hisappr
oachappear
aspr
ogr
amsgr
ow l
argerandmor
ecompl
ex.Tomanagei
ncr
eas
ingcompl
exi
ty,
JavaPr
ogr
ammi
ng DeptofCSE,
VVI
T Page1
UNI
T1 I
ntr
oduct
iont
oOOP

t
hes
econdappr
oach,
cal
ledobj
ect
-or
ient
edpr
ogr
ammi
ng,
wasconcei
ved.
 Obj
ect
-or
ient
ed pr
ogr
ammi
ngor
gani
zesapr
ogr
am ar
ound i
tsdat
a(t
hati
s,
obj
ect
s)andas
etofwel
l-
def
inedmet
hodst
othatdat
a.Anobj
ect
-or
ient
ed
pr
ogr
am canbechar
act
eri
zedasdat
acont
rol
li
ngacces
stocode.

Dat
aAbs
tract
ion:Abs
tract
ionr
efer
stot
heactofr
epr
esent
inges
sent
ialf
eat
ures
wi
thouti
ncl
udi
ngt
hebackgr
ounddet
ail
sorexpl
anat
ions
.Cl
ass
esus
etheconcept
ofabs
tract
ion.

 Pr
inci
plesofOOP:
Thef
oll
owi
ngar
ethe3pr
inci
plesofOOP

1.Encaps
ulat
ion
2.I
nher
itance
3.Pol
ymor
phi
sm

1.Encaps
ulat
ion:
Wrappi
ngupoft
hedat
aandmet
hodsi
ntoas
ingl
euni
tisknown
asEncaps
ulat
ion.Encaps
ulat
ioni
sthemechani
smt
hatbi
ndst
oget
hercodeand
t
hedat
aitmani
pul
ates
,andkeepsbot
hsaf
efr
om out
sidei
nter
fer
enceand
mi
sus
e.

I
nJavat
hebas
isofencaps
ulat
ioni
sthecl
ass
.Acl
assdef
inest
hes
truct
ureand
behavi
or(
dat
aandcode)t
hatwi
llbes
har
edbyas
etofobj
ect
s.Eachobj
ectofa
gi
vencl
asscont
ainst
hes
truct
ureandbehavi
ordef
inedbyt
hecl
ass
,asi
fitwer
e
s
tampedoutbyamol
dint
hes
hapeoft
hecl
ass
.Fort
hisr
eas
on,obj
ect
sar
e
s
omet
imesr
efer
redt
oasi
nst
ancesofacl
ass
.Thus
,acl
assi
sal
ogi
calcons
truct
;
anobj
ecthasphys
icalr
eal
it
y.

2.I
nher
itance:
Inher
itancei
sthepr
oces
sbywhi
chobj
ect
sofonecl
assacqui
ret
he
pr
oper
tiesofobj
ect
sofanot
hercl
ass
.Acl
assi
nher
itss
tat
eandbehavi
orf
rom i
ts
s
uper
clas
s.I
nher
itance pr
ovi
des a power
fuland nat
uralmechani
sm f
or
or
gani
zingands
truct
uri
ngs
oft
war
epr
ogr
ams
.

JavaPr
ogr
ammi
ng DeptofCSE,
VVI
T Page2
UNI
T1 I
ntr
oduct
iont
oOOP

Reus
abi
li
tyi
smai
nadvant
agei
ninher
itancebywhi
chwecanaddaddi
ti
onal
f
eat
urest
oanexi
sti
ngcl
asswi
thoutmodi
fyi
ngi
t.Thi
sispos
sibl
ebyder
ivi
nga
newcl
assf
rom t
heexi
sti
ngone.Thenewcl
asswi
llhavet
hecombi
nedf
eat
ures
ofbot
hthecl
ass
es.

3.Pol
ymor
phi
sm:Pol
ymor
phi
sm meanst
heabi
li
tyt
otakemor
ethanonef
orm.
Forexampl
e,anoper
ati
onmayexhi
bitdi
ff
erentbehavi
ori
ndi
ff
erenti
nst
ances
.
The behavi
ordependsupon t
he t
ypesofdat
a us
ed i
nthe oper
ati
on.For
exampl
e,cons
idert
heoper
ati
onofaddi
ti
on.Fort
wonumber
s,t
heoper
ati
on
wi
llgener
ateas
um.I
ftheoper
andsar
est
rings
,then t
heoper
ati
on woul
d
pr
oduceat
hir
dst
ringbyconcat
enat
ion.

 Dynami
cBi
ndi
ng:
Bindi
ngr
efer
stot
hel
inki
ngofapr
ocedur
ecal
ltot
hecodet
o
beexecut
edi
nres
pons
etot
hecal
l.Dynami
cBi
ndi
ngi
sthatt
hecodeas
soci
ated
wi
thagi
venpr
ocedur
alcal
lisnotknownunt
ilt
het
imeofcal
latr
unt
ime.Dynami
c
Bi
ndi
ngi
sas
soci
atedwi
thpol
ymor
phi
sm andi
nher
itance.

 Mes
sageCommuni
cat
ion:
AnOOPcons
ist
sofas
etofobj
ect
sthatcommuni
cat
e
wi
theachot
her
.Thepr
oces
singofpr
ogr
ammi
ngi
nanOO l
anguagei
nvol
vest
he
f
oll
owi
ngs
teps
:

1)Cr
eat
ingcl
ass
est
hatdef
ineobj
ect
sandt
hei
rbehavi
or.
2)Cr
eat
ingObj
ect
sfor
m cl
assdef
ini
ti
ons
.
3)Es
tabl
ishi
ngcommuni
cat
ionamongobj
ect
s.

Obj
ect
scommuni
cat
ewi
thoneanot
herbys
endi
ngandr
ecei
vingi
nfor
mat
ionmuch
t
hes
amewayaspeopl
emes
sagest
ooneanot
her
.

 Pr
ocedur
eor
ient
edPr
ogr
ammi
ngVsObj
ectOr
ient
edPr
ogr
ammi
ng
 Pr
ocedur
eor
ient
edPr
ogr
ammi
ng:
 I
nthi
sappr
oach,t
hepr
obl
em i
sal
wayscons
ider
edasas
equenceoft
askst
obe
done.
Anumberoff
unct
ionsar
ewr
itt
ent
oaccompl
isht
hes
etas
ks.
Her
epr
imar
y

JavaPr
ogr
ammi
ng DeptofCSE,
VVI
T Page3
UNI
T1 I
ntr
oduct
iont
oOOP

f
ocusonFunct
ionsandl
it
tleat
tent
ionondat
a.
 Ther
ear
emanyhi
ghl
evell
anguagesl
ikeCOBOL,FORTRAN,PASCAL,Cus
edf
or
convent
ional pr
ogr
ammi
ng commonl
y known as Pr
ocedur
e or
ient
ed
Pr
ogr
ammi
ng.
 Pr
ocedur
e or
ient
ed Pr
ogr
ammi
ng bas
ical
ly cons
ist
s of wr
iti
ng a l
ist of
i
nst
ruct
ionsf
ort
hecomput
ert
ofol
low,andor
gani
zingt
hes
eins
truct
ionsi
nto
gr
oupsknownasf
unct
ions
.

Char
act
eri
sti
csofPOP:
 Emphas
isi
sondoi
ngact
ions
.
 Lar
gepr
ogr
amsar
edi
videdi
ntos
mal
lerpr
ogr
amsknownasf
unct
ions
.
 Mos
toft
hef
unct
ionss
har
edgl
obaldat
a.
 Dat
amoveopenl
yar
oundt
hepr
ogr
am f
rom f
unct
iont
ofunct
ion.
 Funct
ionst
rans
for
m dat
afr
om onef
ormt
oanot
her
.
 Empl
oyst
op-
downappr
oachi
npr
ogr
am des
ign.

 OOP:OOPal
lowsust
odecompos
eapr
obl
em i
ntoanumberofent
iti
escal
led
obj
ect
sandt
henbui
ldsdat
aandmet
hodsar
oundt
hes
eent
iti
es.

Def
:OOPi
sanappr
oacht
hatpr
ovi
desawayofmodul
ari
zingpr
ogr
amsbycr
eat
ing
por
tionedmemor
yar
eaf
orbot
hdat
aandmet
hodst
hatcanus
edast
empl
atesf
or
cr
eat
ingcopi
esofs
uchmodul
esondemand.
OOPChar
act
eri
sti
cs:
 Emphas
isondat
a.
 Pr
ogr
amsar
edi
videdi
ntowhatar
eknownasmet
hods
.
 Dat
ast
ruct
uresar
edes
igneds
ucht
hatt
heychar
act
eri
zet
heobj
ect
s.
 Met
hodst
hatoper
ateont
hedat
aofanobj
ectar
eti
edt
oget
her
.
 Dat
aishi
dden.
 Obj
ect
scancommuni
cat
ewi
theachot
hert
hroughmet
hods
.

JavaPr
ogr
ammi
ng DeptofCSE,
VVI
T Page4
UNI
T1 I
ntr
oduct
iont
oOOP

 Reus
abi
li
ty.
 Fol
lowsbot
tom-
upappr
oachi
npr
ogr
am des
ign.

Appl
icat
ionsofOOPs
:
 Realt
imes
yst
ems  Neur
al net
wor
ks and par
all
el
 Si
mul
ati
onandmodel
ing pr
ogr
ammi
ng
 OOdat
abas
e  Deci
sion s
uppor
t and of
fi
ce
 Hyper
text
,hyper
medi
aandexper
t- aut
omat
ions
yst
ems
t
ext  CAM/
CADs
yst
ems
.
 AIandexper
tsys
tems
Advant
agesofOOPs
:

 Thi
sel
imi
nat
esr
edundantcodeandext
endst
heus
eofcl
ass
eswi
tht
heconcept
ofi
nher
itance.
 Thi
s can bui
ldt
he pr
ogr
ams f
rom t
he s
tandar
d wor
king modul
es t
hat
communi
cat
ewi
thoneanot
her
,rat
hert
hanhavi
ngt
ost
artwr
iti
ngt
hecode
f
rom begi
nni
ng.Thi
sleads t
osavi
ng of devel
opment t
ime and hi
gher
pr
oduct
ivi
ty.
 Thepr
inci
pleofdat
ahi
dinghel
pst
hepr
ogr
ammert
obui
lds
ecur
epr
ogr
ams
t
hatcannotbei
nvadedbycodei
not
herpar
tsoft
hepr
ogr
am.
 I
tispos
sibl
eto have mul
ti
plei
nst
ance ofan obj
ectt
o exi
stwi
thoutany
i
nter
fer
ence.
 Sof
twar
ecompl
exi
tycanbemanaged.
 OOs
yst
emscanbeeas
ilyupgr
adedf
rom s
mal
ltol
arges
yst
ems
.

 Hi
stor
yofJAVA:
Javai
srel
atedt
oC++,whi
chi
sadi
rectdes
cendantofC.Much
oft
hechar
act
erofJavai
sinher
itedf
rom t
hes
etwol
anguages
.Fr
om C,Javader
ives
i
tss
ynt
ax.ManyofJava’
sobj
ect
-or
ient
edf
eat
ureswer
einf
luencedbyC++.I
nfact
,
s
ever
alofJava’
sdef
ini
ngchar
act
eri
sti
cscomef
rom—orar
eres
pons
est
o—i
ts
pr
edeces
sor
s.Mor
eover
,thecr
eat
ionofJavawasdeepl
yroot
edi
nthepr
oces
sof

JavaPr
ogr
ammi
ng DeptofCSE,
VVI
T Page5
UNI
T1 I
ntr
oduct
iont
oOOP

r
efi
nementand adapt
ati
on t
hathasbeen occur
ringi
n comput
erpr
ogr
ammi
ng
l
anguagesf
ort
hepas
tsever
aldecades
.

Byt
heendoft
he1980sandt
heear
ly1990s
,obj
ect
-or
ient
edpr
ogr
ammi
ngus
ing
C++t
ookhol
d.I
ndeed,f
orabr
iefmomenti
tseemedasi
fpr
ogr
ammer
shadf
inal
ly
f
oundt
heper
fectl
anguage.Becaus
eC++bl
endedt
hehi
ghef
fi
ciencyands
tyl
ist
ic
el
ement
sofCwi
tht
heobj
ect
-or
ient
edpar
adi
gm,i
twasal
anguaget
hatcoul
dbe
us
edt
ocr
eat
eawi
der
angeofpr
ogr
ams
.However
,jus
tasi
nthepas
t,f
orceswer
e
br
ewi
ngt
hatwoul
d,onceagai
n,dr
ivecomput
erl
anguageevol
uti
onf
orwar
d.Wi
thi
n
af
ew year
s,t
heWor
ldWi
deWebandt
heI
nter
netwoul
dreachcr
iti
calmas
s.Thi
s
eventwoul
dpr
eci
pit
ateanot
herr
evol
uti
oni
npr
ogr
ammi
ng.

Ther
ewer
efi
vepr
imar
ygoal
sint
hecr
eat
ionoft
heJaval
anguage

 I
tshoul
dus
ethe 
obj
ect
-or
ient
edpr
ogr
ammi
ng 
met
hodol
ogy.
 I
tshoul
d al
low t
hes
amepr
ogr
am t
o beexecut
ed on mul
ti
pleoper
ati
ng
s
yst
ems
.
 I
tshoul
dcont
ainbui
lt
-i
nsuppor
tforus
ingcomput
ernet
wor
ks.
 I
tshoul
dbedes
ignedt
oexecut
ecodef
rom r
emot
esour
cess
ecur
ely.
 I
tshoul
dbeeas
ytous
ebys
elect
ingwhatwascons
ider
edt
hegoodpar
tsof
ot
herobj
ector
ient
edl
anguages
.

TheCr
eat
ionofJava:Javawasconcei
vedbyJamesGos
ling,Pat
rickNaught
on,
Chr
isWar
th,EdFr
ank,andMi
keSher
idanatSunMi
cros
yst
ems
,Inc.i
n1991.I
ttook
18mont
hst
odevel
opt
hef
irs
twor
kingver
sion.Thi
slanguagewasi
nit
ial
lycal
led
“Oak,
”butwasr
enamed“Java”i
n1995.

Bet
ween t
hei
nit
iali
mpl
ement
ati
on ofOaki
nthef
allof1992 and t
hepubl
ic

JavaPr
ogr
ammi
ng DeptofCSE,
VVI
T Page6
UNI
T1 I
ntr
oduct
iont
oOOP

announcementofJavai
nthes
pri
ngof1995,manymor
epeopl
econt
ribut
edt
othe
des
ignandevol
uti
onoft
hel
anguage.Bi
llJoy,Ar
thurvanHo,Jonat
hanPayne,
Fr
ankYel
li
n,and Ti
m Li
ndhol
m wer
ekeycont
ribut
orst
othemat
uri
ngoft
he
or
igi
nalpr
otot
ype.

Javawasdes
ignednotf
ort
heI
nter
net
,thepr
imar
ymot
ivat
ionwast
heneedf
ora
pl
atf
orm-
independent(
thati
s,ar
chi
tect
ure-neut
ral
)languaget
hatcoul
dbeus
edt
o
cr
eat
esof
twar
etobeembeddedi
nvar
iouscons
umerel
ect
roni
cdevi
ces
,suchas
mi
crowaveovensandr
emot
econt
rol
s.Thet
roubl
ewi
thCandC++(
andmos
tot
her
l
anguages
)ist
hatt
heyar
edes
ignedt
obecompi
ledf
oras
peci
fi
ctar
get
.Al
thoughi
t
i
spos
sibl
etocompi
leaC++pr
ogr
am f
orj
ustaboutanyt
ypeofCPU,t
odos
o
r
equi
resaf
ullC++compi
lert
arget
edf
ort
hatCPU.Thepr
obl
em i
sthatcompi
ler
s
ar
e expens
ive and t
ime-
cons
umi
ng t
o cr
eat
e. An eas
ier
—and mor
e cos
t
ef
fi
cient
—sol
uti
onwasneeded.

I
nanat
temptt
ofi
nds
uchas
olut
ion,Gos
lingandot
her
sbeganwor
konapor
tabl
e,
pl
atf
orm-
independentl
anguaget
hatcoul
dbeus
edt
opr
oducecodet
hatwoul
drun
onavar
iet
yofCPUsunderdi
ff
eri
ngenvi
ronment
s.Thi
sef
for
tul
ti
mat
elyl
edt
othe
cr
eat
ionofJava.

Becaus
eoft
hes
imi
lar
iti
esbet
weenJavaandC++,Javas
houl
dnotbecons
ider
edas
t
he“I
nter
netver
sionofC++.
”Javahass
igni
fi
cantdi
ff
erences
.Event
houghJava
wasi
nfl
uencedbyC++,i
tisnotanenhancedver
sionofC++.Forexampl
e,Javai
s
nei
therupwar
dlynordownwar
dlycompat
ibl
ewi
thC++.Ofcour
se,t
hes
imi
lar
iti
es
wi
thC++ar
esi
gni
fi
cant
.Oneot
herpoi
nt:Javawasnotdes
ignedt
orepl
aceC++.
Javawasdes
ignedt
osol
veacer
tai
nsetofpr
obl
ems
.C++wasdes
ignedt
osol
vea
di
ff
erents
etofpr
obl
ems
.Bot
hwi
llcoexi
stf
ormanyyear
stocome.

 JavaandI
nter
net
:TheI
nter
nethel
pedJavat
othef
oref
rontofpr
ogr
ammi
ng,
and
Java,i
ntur
n,hadapr
ofoundef
fectont
heI
nter
net
.Inaddi
ti
ont
osi
mpl
if
yingweb
pr
ogr
ammi
ngi
ngener
al,Javai
nnovat
edanew t
ypeofnet
wor
kedpr
ogr
am cal
led

JavaPr
ogr
ammi
ng DeptofCSE,
VVI
T Page7
UNI
T1 I
ntr
oduct
iont
oOOP

heappl
t ett
hatchangedt
hewayt
heonl
inewor
ldt
houghtaboutcont
ent
.Javaal
so
addr
ess
eds
omeoft
hei
ssuesas
soci
atedwi
tht
heI
nter
net
:por
tabi
li
tyands
ecur
ity.

1.JavaAppl
ets
:Anappl
eti
sas
peci
alki
ndofJavapr
ogr
am t
hati
sdes
ignedt
o
bet
rans
mit
ted overt
heI
nter
netand aut
omat
ical
lyexecut
ed byaJava-
compat
ibl
ewebbr
ows
er.Fur
ther
mor
e,anappl
eti
sdownl
oadedondemand,
wi
thoutf
urt
heri
nter
act
ionwi
tht
heus
er.
Ift
heus
ercl
icksal
inkt
hatcont
ains
an appl
et,t
heappl
etwi
llbeaut
omat
ical
lydownl
oaded and r
un i
nthe
br
ows
er.Appl
etsar
eint
endedt
obes
mal
lpr
ogr
ams
.Thecr
eat
ionoft
he
appl
etchangedI
nter
netpr
ogr
ammi
ngbecaus
eitexpandedt
heuni
ver
seof
obj
ect
sthatcanmoveaboutf
reel
yincyber
space.
2.Secur
ity:Asyou ar
eli
kel
yawar
e,ever
yti
meyou downl
oad a“nor
mal

pr
ogr
am,youar
etaki
ngar
isk,becaus
ethecodeyouar
edownl
oadi
ngmi
ght
cont
ain avi
rus
,Tr
ojan hor
se,orot
herhar
mfulcode.Att
hecor
eoft
he
pr
obl
em i
sthef
actt
hatmal
ici
ouscodecancaus
eit
sdamagebecaus
eithas
gai
ned unaut
hor
ized acces
stos
yst
em r
esour
ces
.Forexampl
e,a vi
rus
pr
ogr
am mi
ghtgat
herpr
ivat
einf
ormat
ion,s
uch ascr
edi
tcar
d number
s,
bankaccountbal
ances
,andpas
swor
ds,bys
ear
chi
ngt
hecont
ent
sofyour
comput
er’
slocalf
il
esys
tem.Javaachi
evedt
hispr
otect
ionbyconni
ngan
appl
ett
otheJavaexecut
ionenvi
ronmentandnotal
lowi
ngi
tacces
stoot
her
par
tsoft
hecomput
er.
3.Por
tabi
li
ty:Por
tabi
li
tyi
samaj
oras
pectoft
heI
nter
netbecaus
ether
ear
e
manydi
ff
erentt
ypesofcomput
ersandoper
ati
ngs
yst
emsconnect
edt
oit
.If
aJavapr
ogr
am wer
etober
unonvi
rtual
lyanycomput
erconnect
edt
othe
I
nter
net
,ther
eneededt
obes
omewayt
oenabl
ethatpr
ogr
am t
oexecut
eon
di
ff
erents
yst
ems
.Forexampl
e,i
nthecas
eofanappl
et,t
hes
ameappl
et
mus
tbeabl
etobedownl
oadedandexecut
edbyt
hewi
devar
iet
yofCPUs
,
oper
ati
ngs
yst
ems
,andbr
ows
ersconnect
edt
otheI
nter
net
.Iti
snotpr
act
ical
t
ohavedi
ff
erentver
sionsoft
heappl
etf
ordi
ff
erentcomput
ers
.Thes
ame
codemus
twor
konal
lcomput
ers
.

JavaPr
ogr
ammi
ng DeptofCSE,
VVI
T Page8
UNI
T1 I
ntr
oduct
iont
oOOP

 JavaVi
rtualMachi
ne:

Byt
eCode:Thekeyt
hatal
lowsJavat
osol
vebot
hthes
ecur
ityandt
hepor
tabi
li
ty
pr
obl
emsi
sthatt
heout
putofaJavacompi
leri
snotexecut
abl
ecode.Rat
her
,iti
s
byt
ecode.

Byt
ecodei
sahi
ghl
yopt
imi
zeds
etofi
nst
ruct
ionsdes
ignedt
obeexecut
edbyt
he
Javar
un-
ti
mes
yst
em,whi
chi
scal
ledt
heJavaVi
rtualMachi
ne(
JVM)
.Ines
sence,
t
heor
igi
nalJVMwasdes
ignedasani
nter
pret
erf
orbyt
ecode.

Tr
ans
lat
ingaJavapr
ogr
am i
ntobyt
ecodemakesi
tmucheas
iert
orunapr
ogr
am i
n
awi
devar
iet
yofenvi
ronment
sbecaus
eonl
ytheJVMneedst
obei
mpl
ement
edf
or
each pl
atf
orm.Oncet
her
un-
ti
mepackageexi
stsf
oragi
ven s
yst
em,anyJava
pr
ogr
am canr
unoni
t.Remember
,al
thought
hedet
ail
soft
heJVM wi
lldi
ff
erf
rom
pl
atf
ormt
opl
atf
orm,al
lunder
standt
hes
ameJavabyt
ecode.Thus
,theexecut
ion
ofbyt
ecodebyt
heJVMi
stheeas
ies
twayt
ocr
eat
etr
ulypor
tabl
epr
ogr
ams
.

AJavapr
ogr
am i
sexecut
edbyt
heJVM al
sohel
pst
omakei
tsecur
e.Becaus
ethe
JVM i
sincont
rol
,itcancont
aint
hepr
ogr
am andpr
eventi
tfr
om gener
ati
ngs
ide
ef
fect
sout
sideoft
hes
yst
em.Asyouwi
lls
ee,s
afet
yisal
soenhancedbycer
tai
n
r
est
rict
ionst
hatexi
sti
ntheJaval
anguage.

I
n gener
al,when a pr
ogr
am i
scompi
led t
o an i
nter
medi
atef
orm and t
hen
i
nter
pret
edbyavi
rtualmachi
ne,i
trunss
lowert
hani
twoul
druni
fcompi
ledt
o
execut
abl
ecode.However
,wi
thJava,t
hedi
ff
erent
ialbet
weent
het
woi
snots
o
gr
eat
.Becaus
ebyt
ecodehasbeenhi
ghl
yopt
imi
zed,t
heus
eofbyt
ecodeenabl
es
t
heJVMt
oexecut
epr
ogr
amsmuchf
ast
ert
hanyoumi
ghtexpect
.

Jus
t-I
n-Ti
me(
JIT)compi
ler
:When aJI
T compi
leri
spar
toft
heJVM,s
elect
ed
por
tionsofbyt
ecodear
ecompi
ledi
ntoexecut
abl
ecodei
nrealt
ime,onapi
ece-
by-
pi
ece,demandbas
is.I
tisi
mpor
tantt
ounder
standt
hati
tisnotpr
act
icalt
ocompi
le
anent
ireJavapr
ogr
am i
ntoexecut
abl
ecodeal
latonce,becaus
eJavaper
for
ms

JavaPr
ogr
ammi
ng DeptofCSE,
VVI
T Page9
UNI
T1 I
ntr
oduct
iont
oOOP

var
iousr
un-
ti
mecheckst
hatcanbedoneonl
yatr
unt
ime.JI
Tcompi
lespar
tsoft
he
byt
ecodet
hathaves
imi
larf
unct
ional
it
yatt
hes
amet
ime,andhencer
educest
he
amountoft
imeneededf
orcompi
lat
ion.
Iti
sus
edt
oimpr
ovet
heper
for
mance.

 JavaFeat
ures
:TheJaval
anguagewasi
nvent
ednotonl
yforpor
tabi
li
tyand
s
ecur
ity,butal
sot
her
ear
emanyot
herf
eat
urespl
ayedani
mpor
tantr
ole.The
f
oll
owi
ngar
ethef
eat
uresofj
ava
a)Si
mpl
e,Smal
landFami
li
ar:Javai
sas
impl
eandf
ami
li
arl
anguagebecaus
eit
cont
ainsmanyf
eat
uresofot
herLanguagesl
ikecandC++.Javar
emovescompl
exi
ty
becaus
eitdoes
n’tus
epoi
nter
s,s
tor
agecl
ass
esandgot
ost
atement
sandal
soj
ava
does
n’ts
uppor
tMul
ti
pleI
nher
itanceandoper
atorover
loadi
ng.
b)Compi
ledandI
nter
pret
ed:
Javacombi
nest
hef
eat
uresofbot
hcompi
ledand
i
nter
pret
ed appr
oaches
.Fi
rst
,Javacompi
lert
rans
lat
ess
our
cecodei
nto
whati
sknown asbyt
e code i
nst
ruct
ions
.Byt
e codesar
e notmachi
ne
i
nst
ruct
ions
.Second,Javai
nter
pret
ergener
atesmachi
necodet
hatcanbe
di
rect
lyexecut
edbyt
hemachi
net
hati
srunni
ngt
hej
avapr
ogr
am.Javai
s
bot
hcompi
ledandi
nter
pret
edl
anguage.
c)Pl
atf
orm-
Independentand Por
tabl
e:Javapr
ogr
amscan beeas
ilymoved
f
rom onecomput
ers
yst
em t
oanot
her
,anywher
eandanyt
ime.Changesand
upgr
adesi
noper
ati
ngs
yst
ems
,pr
oces
sor
sands
yst
em r
esour
ceswi
llnot
f
orceanychangesi
njavapr
ogr
ams
.Javaens
urespor
tabi
li
tyi
ntwoways
.
Fi
rst
, Java compi
ler gener
ates byt
e code i
nst
ruct
ion t
hat can be
i
mpl
ement
edonanymachi
ne.Secondl
y,t
hes
izesoft
hepr
imi
ti
vedat
atypes
ar
emachi
nei
ndependent
.
d)Obj
ect
-Or
ient
ed:Javai
sat
rueobj
ect
-or
ient
edl
anguage.Al
mos
tever
ythi
ng
i
njavai
sanobj
ect
.Al
lpr
ogr
am codeanddat
ares
idewi
thi
nobj
ect
sand
cl
ass
es.
Javacomeswi
thanext
ens
ives
etofcl
ass
es,
arr
angedi
npackages
.
e)Robus
tandSecur
e:Javai
sar
obus
tlanguage.I
tpr
ovi
desmanys
afeguar
dst
o
ens
urer
eli
abl
ecode.
Ithass
tri
ctcompi
let
imeandr
unt
imechecki
ngf
ordat
a
t
ypes
.Iti
s des
igned as a gar
bage-
col
lect
ed l
anguage r
eli
evi
ng t
he
pr
ogr
ammer
s vi
rtual
ly al
lmemor
y management pr
obl
ems
.Java al
so
JavaPr
ogr
ammi
ng DeptofCSE,
VVI
T Page10
UNI
T1 I
ntr
oduct
iont
oOOP

i
ncor
por
atest
heconcept
sofexcept
ionhandl
ingwhi
chcapt
uress
eri
eser
ror
s
andel
imi
nat
esanyr
iskofcr
ashi
ngt
hes
yst
em.Javas
yst
emsnotonl
yver
if
y
al
lmemor
yacces
sbutal
soens
uret
hatnovi
rus
esar
ecommuni
cat
edwi
than
appl
et.Theabs
enceofpoi
nter
sinj
avaens
urest
hatpr
ogr
amscannotgai
n
acces
stomemor
ylocat
ionswi
thoutpr
operaut
hor
izat
ion.
f
) Di
str
ibut
ed:Java i
s des
igned as a di
str
ibut
ed l
anguage f
or cr
eat
ing
appl
icat
ionsonnet
wor
ks.I
thast
heabi
li
tyt
oshar
ebot
hdat
aandpr
ogr
ams
.
Javaappl
icat
ionscanopenandacces
sremot
eobj
ect
sonI
nter
netaseas
ily
ast
heycan do i
nlocals
yst
em.Thi
senabl
esmul
ti
ple pr
ogr
ammer
sat
mul
ti
pler
emot
elocat
ionst
o col
labor
ateand wor
ktoget
heron as
ingl
e
pr
oject
.
g)Mul
ti
thr
eadedandI
nter
act
ive:
Mul
ti
thr
eadedmeanshandl
ingmul
ti
plet
asks
s
imul
taneous
ly.Javas
uppor
tsmul
ti
thr
eadedpr
ogr
ammi
ng.Thi
smeanst
hat
wenotwai
tfort
heappl
icat
iont
ofi
nis
honet
askbef
orebegi
nni
nganot
her
.
The j
ava r
unt
ime comes wi
th t
ool
s t
hat s
uppor
t mul
ti
proces
s
s
ynchr
oni
zat
ionandcons
tructs
moot
hlyr
unni
ngi
nter
act
ives
yst
ems
.
h)Hi
gh Per
for
mance:Java per
for
mance i
simpr
ess
ive f
or an i
nter
pret
ed
l
anguage,mai
nlyduet
otheus
eofi
nter
medi
atebyt
ecode.Javas
peedi
s
compar
abl
etot
henat
iveC/
C++.Javaar
chi
tect
urei
sal
sodes
ignedt
oreduce
t
heover
headsdur
ingr
unt
ime.Fur
ther
,thei
ncor
por
ati
onofmul
ti
thr
eadi
ng
enhancest
heover
allexecut
ions
peedofj
avapr
ogr
ams
.
i
) Dynami
cand Ext
ens
ibl
e:Javai
sadynami
clanguage.Javai
scapabl
eof
dynami
cal
lyl
inki
nganewcl
assl
ibr
ari
es,
met
hods
,andobj
ect
s.Javacanal
so
det
ermi
ne t
he t
ype ofcl
asst
hrough a quer
y,maki
ng i
t pos
sibl
eto
dynami
cal
lyl
inkorabor
tthepr
ogr
am,dependi
ngont
her
espons
e.Java
pr
ogr
amss
uppor
tfunct
ionswr
itt
eni
not
herl
anguagess
uchasCandC++.
Thes
efunct
ionsar
eknownasnat
ivemet
hods
.Nat
ivemet
hodsar
eli
nked
dynami
cal
lyatr
unt
ime.

JavaPr
ogr
ammi
ng DeptofCSE,
VVI
T Page11
UNI
T1 I
ntr
oduct
iont
oOOP

 JavaPr
ogr
am St
ruct
ure:Aj
avapr
ogr
am maycont
ainmanycl
ass
esofwhi
ch
onl
yonecl
assdef
inesamai
nmet
hod.Cl
ass
escont
aindat
amember
sandmet
hods
t
hatoper
ateont
hedat
amember
soft
hecl
ass
.Met
hodsmaycont
aindat
atype
decl
arat
ionsandexecut
abl
est
atement
s.TheJavapr
ogr
am cont
ainst
hes
ect
ionsas
i
nfol
lowi
ngf
igur
e.

Fi
g:Gener
alSt
ruct
ureofaJavaPr
ogr
am

a)Document
ati
on Sect
ion:Thi
sSect
ion compr
isesa s
etofcommentl
ines
gi
vingt
henameoft
hepr
ogr
am,t
heaut
horandot
herdet
ail
s.Comment
s
mus
texpl
ainwhyandwhatofcl
ass
es.Javaus
est
hrees
tyl
esofcomment
s.
ss
Onei i
ngl
e-l
inecomment(
//)
,Secondonei
smul
ti
-l
inecomment(
/*…*/
)
andt
het
hir sdocument
donei ati
oncomment(
/**…*/
).
b)PackageSt
atement
:Thef
irs
tst
atemental
lowedi
naj
avaf
il
eisapackage
s
tat
ement
.Thi
sst
atementdecl
ares a package name and i
nfor
ms t
he
compi
lert
hatt
hecl
ass
esdef
ined her
ebel
ongt
othi
spackage.Exampl
e:
packages
tudent
;ThePackages
tat
ementi
sopt
ional
.
c)I
mpor
tSt
atement
s:Af
terapackages
tat
ementt
her
ecanbeanynumberof
i
mpor
tst
atement
s.Thi
siss
imi
lart
othe#i
ncl
udes
tat
ementi
nC.Exampl
e:
i
mpor
tjava.
uti
l.
Scanner
;.Thi
sst
atementi
nst
ruct
sthei
nter
pret
ert
oloadt
he
Scannercl
asscont
ainedi
nthepackagej
ava.
uti
l.Us
ingi
mpor
tst
atement
s,

JavaPr
ogr
ammi
ng DeptofCSE,
VVI
T Page12
UNI
T1 I
ntr
oduct
iont
oOOP

wecanhaveacces
stocl
ass
est
hatar
epar
tofot
hernamedpackages
.
d)I
nter
faceSt
atement
s:Ani
nter
facei
sli
keacl
assbuti
ncl
udesagr
oupof
met
hoddecl
arat
ions
.Thi
sisanopt
ionals
ect
ion.
e)Cl
assDef
ini
ti
ons
:A j
avapr
ogr
am maycont
ain mul
ti
plecl
assdef
ini
ti
ons
.
Cl
ass
esar
ethepr
imar
yandes
sent
ialel
ement
sofaj
avapr
ogr
am.Thes
e
cl
ass
esar
eus
edt
omapt
heobj
ect
sofr
eal
-wor
ldpr
obl
ems
.
f
) Mai
nMet
hodCl
ass
:Ever
yst
and-
alonepr
ogr
am r
equi
resamai
nmet
hodasi
ts
s
tar
tingpoi
nt;t
hiscl
assi
sthees
sent
ialpar
tofaj
avapr
ogr
am.Themai
n
met
hodcr
eat
esobj
ect
sofvar
iouscl
ass
esandes
tabl
ishescommuni
cat
ions
bet
weent
hem.Onr
eachi heendofmai
ngt n,t
hepr
ogr
am t
ermi
nat
esandt
he
cont
rolpas
sesbackt
otheoper
ati
ngs
yst
em.

JavaPr
ogr
ammi
ng DeptofCSE,
VVI
T Page13

You might also like