0% found this document useful (0 votes)
2 views27 pages

Inheritance java

Inheritance is a programming mechanism that allows one class to inherit properties and behaviors from another existing class, enhancing code reusability and simplifying program logic. It is widely used in object-oriented programming languages like Java and Python, enabling the creation of hierarchical class structures. The document discusses various types of inheritance, including single, multiple, and hierarchical inheritance, as well as the advantages and implementation details in Java.

Uploaded by

dsp01097
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views27 pages

Inheritance java

Inheritance is a programming mechanism that allows one class to inherit properties and behaviors from another existing class, enhancing code reusability and simplifying program logic. It is widely used in object-oriented programming languages like Java and Python, enabling the creation of hierarchical class structures. The document discusses various types of inheritance, including single, multiple, and hierarchical inheritance, as well as the advantages and implementation details in Java.

Uploaded by

dsp01097
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 27

I

nher
it
ance
I
nher
it
ancei
sthepr
ocessofbui
l
dinganewcl
assbasedont
hef
eat
uresofanot
herexi
sti
ng
cl
ass.I
tisusedheav
il
yinJav
a,Py
thon,
andot
herobj
ect
-or
ient
edl
anguagest
oincr
easecode
r
eusabi
l
ityandsi
mpl
i
fypr
ogr
am l
ogi
cint
ocat
egor
ical
andhi
erar
chi
cal
rel
ati
onshi
ps.

Whati
sInher
it
ance?

I
nher
it
ancei
samechani
sm t
hatal
l
owsonecl
asst
oinher
itpr
oper
ti
esorbehav
ior
sfr
om
anot
hercl
ass.Mul
ti
plecl
assescani
nher
itf
rom t
hesamepar
entcl
ass,
for
mingat
ree-
li
ke
hi
erar
chyst
ruct
ure.I
nher
it
ingcl
assescanaddf
eat
uresbey
ondt
hosei
nher
it
edf
rom t
hepar
ent
cl
asst
oal
l
owf
oruni
quebehav
ior
.

I
nher
it
ancei
sessent
ial
toadv
ancedObj
ectOr
ient
edPr
ogr
ammi
ng(
OOP)asi
tal
l
owsy
out
o
r
euseonecl
ass’
sfeat
uresacr
ossy
ourpr
ogr
am wi
thoutr
epl
i
cat
ingcode.

I
nher
it
ancei
sof
tenusedt
orepr
esentcat
egor
ies(
par
entcl
asses)andsub-
cat
egor
ies
(
subcl
asses)
.Thepar
entcl
assset
sthef
eat
urespr
esenti
nal
lobj
ect
sregar
dlessof
subcat
egor
y,whi
l
eeachsubcl
assr
epr
esent
sasmal
l
er,
mor
especi
fi
ccat
egor
y.

I
nher
it
ancehast
hreemai
nadv
ant
ages:

Reusabi
l
ity
:

I
nher
it
anceal
l
owsy
out
oreuset
hef
eat
uresofanexi
sti
ngcl
assanunl
i
mit
ednumberoft
imes
acr
ossanycl
asst
hati
nher
it
sthatcl
ass.Youcankeepconsi
stentf
unct
ional
i
tyacr
ossal
l
obj
ect
soft
hesamet
ypewi
thoutr
ewr
it
ingt
hecode.

CodeSt
ruct
ure:

I
nher
it
ancepr
ovi
desacl
ear
,dr
awabl
elogi
cst
ruct
uref
ory
ourpr
ogr
am.I
tal
l
owsdev
eloper
sto
under
standy
ourcodeasacol
l
ect
ionofr
elat
edbutuni
quecat
egor
iesr
athert
hansi
mpl
yabl
ock
ofcode.
Dat
aHi
ding:

Thebasecl
asscanbesett
okeepsomedat
apr
ivat
esot
hati
tcannotbeal
ter
edbyt
heder
ived
cl
ass.Thi
sisanexampl
eofencapsul
ati
on,
wher
eaccesst
odat
aisr
est
ri
ctedt
oonl
ythe
cl
assest
hatneedi
tfort
hei
rrol
e.

I
nher
it
ancei
nJav
a

Eachpr
ogr
ammi
ngl
anguagehassl
i
ght
lydi
ff
erentt
ermi
nol
ogyf
ori
nher
it
ance.I
nJav
a,t
he
par
entcl
assi
scal
l
edt
hesuper
class,
andt
hei
nher
it
orcl
assi
scal
l
edt
hesubcl
ass.Dev
eloper
s
mayal
socal
lsuper
classesbaseorpar
entcl
assesandsubcl
assesder
ivedorchi
l
dcl
asses.

Subcl
assesar
eli
nkedt
osuper
classesusi
ngt
heext
endskey
wor
ddur
ingt
hei
rdef
ini
ti
on.
Subcl
assescandef
inenewl
ocal
met
hodsorf
iel
dst
ouseorcanuset
hesuperkey
wor
dtocal
l
i
nher
it
edmet
hodsort
hesuperconst
ruct
or.

cl
assb

{
/
/impl
ement
ati
onofi
nher
it
edMet
hod(
)
}
cl
assaex
tendsb
{
i
nher
it
edMet
hod(
);
}

Whent
ousesuperkey
wor
d

superi
sessent
ial
l
ya“
prev
iousv
alue”but
toncal
l
edf
rom wi
thi
nachi
l
dcl
asst
hatal
l
owsy
out
o
r
eadandaccessf
eat
uresf
rom t
hepar
entcl
assr
egar
dlessoft
hei
rval
uei
nthecur
rentchi
l
d
cl
ass.

Thesuperkey
wor
disusedt
o:

Accesspar
entcl
assf
iel
ds:

super
.varr
eadst
hev
alueofv
arseti
nthepar
entcl
ass,
whi
l
evaral
oner
eadst
hemodi
fi
edv
alue
f
rom t
hechi
l
d.
Cal
l
ingapar
entcl
assmet
hod:

super
.met
hod(
)al
l
owst
hechi
l
dtoaccesst
hepar
entcl
assi
mpl
ement
ati
onofmet
hod(
).Thi
sis
onl
yrequi
redi
fthechi
l
dcl
assal
sohasamet
hodwi
tht
hesamename.

Usi
ngConst
ruct
ors:

Thi
sal
l
owsy
out
ocr
eat
enewi
nst
ancesoft
hepar
entcl
assf
rom wi
thi
nachi
l
dcl
ass.

Asar
efr
esher
,const
ruct
orsi
nJav
aar
especi
almet
hodsusedt
oini
ti
ali
zeobj
ect
s.Cal
l
ingt
he
superconst
ruct
orcr
eat
esanewobj
ectt
hatr
equi
resal
lthef
iel
dsdef
inedi
nthepar
entcl
ass
const
ruct
or.

Youcant
henaddaddi
ti
onal
fiel
dsi
not
herst
atement
stomaket
hechi
l
dinst
ancemor
especi
fi
c
t
hant
hepar
ent
.Essent
ial
l
y,i
tal
l
owsy
out
ouset
hepar
entcl
assconst
ruct
orasat
empl
atef
or
y
ourchi
l
dcl
assconst
ruct
or.

publ
i
cCar
(St
ri
ngmake,
Str
ingcol
or,
inty
ear
,St
ri
ngmodel
,St
ri
ngbody
Sty
le){
super
(make,
col
or,
year
,model
);/
/par
entcl
assconst
ruct
or
t
his.
body
Sty
le=body
Sty
le;
}

Ty
pesofI
nher
it
ance

Ther
ear
esev
eral
typesofi
nher
it
anceav
ail
abl
einJav
a:

Si
ngl
einher
it
ance

i
swhenasi
ngl
esubcl
assi
nher
it
sfr
om asuper
class,
for
mingonel
ayerofi
nher
it
ance.

Mul
ti
lev
elI
nher
it
ance

i
swhenasuper
classi
sinher
it
edbyani
nter
medi
atecl
ass,
whi
chi
stheni
nher
it
edbyader
ived
cl
ass,
for
ming3ormor
elev
elsofi
nher
it
ance.

Hi
erar
chi
cal
inher
it
ance

i
swhenonesuper
classser
vesasabasel
i
nef
ormul
ti
plespeci
fi
csubcl
asses.Thi
sist
hemost
commonf
orm ofi
nher
it
ance.

Ther
ear
eal
sot
woot
hert
ypesofi
nher
it
ancet
hatar
eonl
yav
ail
abl
einJav
athr
ougha
combi
nat
ionofcl
assandi
nter
facei
nher
it
ance.

Mul
ti
plei
nher
it
ance

whenasi
ngl
esubcl
assi
nher
it
sfr
om mul
ti
plepar
entcl
asses.

Hy
bri
dinher
it
ance

ami
xoft
woormor
eoft
heabov
eki
ndsofi
nher
it
ance.

Jav
adoesnotsuppor
tmul
ti
plei
nher
it
ancewi
thcl
asses,
meani
ngbot
hoft
heset
ypesof
i
nher
it
ancear
eimpossi
blewi
thJav
acl
assesal
one.Howev
er,
asubcl
asscani
nher
itmor
ethan
onei
nter
face(
anabst
ractcl
ass)
.Youcant
her
efor
esi
mul
atemul
ti
plei
nher
it
ancei
fyou
combi
net
heuseofi
nter
facesandcl
asses.

Si
ngl
eInher
it
ance:

I
nsi
ngl
einher
it
ance,
subcl
assesi
nher
itt
hef
eat
uresofonesuper
class.I
nimagebel
ow,
the
cl
assAser
vesasabasecl
assf
ort
heder
ivedcl
assB.

/
/Jav
apr
ogr
am t
oil
l
ust
rat
ethe

/
/conceptofsi
ngl
einher
it
ance

i
mpor
tjav
a.i
o.*
;
cl
assone

publ
i
cvoi
dpr
int
_geek(
)

Sy
stem.
out
.pr
int
ln(
"Geeks"
);

cl
asst
woext
endsone

publ
i
cvoi
dpr
int
_for
()

Sy
stem.
out
.pr
int
ln(
"for
");

/
/Dr
ivercl
ass

publ
i
ccl
assM

publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs)

t
wog=newt
wo(
);
g.
pri
nt_
geek(
);

g.
pri
nt_
for
();

g.
pri
nt_
geek(
);

Out
put
:

Geeks

f
or

Geeks

Mul
ti
lev
elI
nher
it
ance:
InMul
ti
lev
elI
nher
it
ance,
ader
ivedcl
asswi
l
lbei
nher
it
inga

basecl
assandaswel
last
heder
ivedcl
assal
soactast
hebasecl
asst
oot
hercl
ass.I
nbel
ow
i
mage,
thecl
assAser
vesasabasecl
assf
ort
heder
ivedcl
assB,
whi
chi
ntur
nser
vesasabase
cl
assf
ort
heder
ivedcl
assC.I
nJav
a,acl
asscannotdi
rect
lyaccesst
hegr
andpar
ent
’smember
s.

/
/Jav
apr
ogr
am t
oil
l
ust
rat
ethe

/
/conceptofMul
ti
lev
eli
nher
it
ance

i
mpor
tjav
a.i
o.*
;
cl
assone

publ
i
cvoi
dpr
int
_geek(
)

Sy
stem.
out
.pr
int
ln(
"Geeks"
);

cl
asst
woext
endsone

publ
i
cvoi
dpr
int
_for
()

Sy
stem.
out
.pr
int
ln(
"for
");

cl
asst
hreeext
endst
wo

publ
i
cvoi
dpr
int
_geek(
)

Sy
stem.
out
.pr
int
ln(
"Geeks"
);
}

/
/Dr
ivedcl
ass

publ
i
ccl
assN

publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs)

t
hreeg=newt
hree(
);

g.
pri
nt_
geek(
);

g.
pri
nt_
for
();

g.
pri
nt_
geek(
);

Out
put
:

Geeks

f
or

Geeks
Hi
erar
chi
cal
Inher
it
ance:

I
nHi
erar
chi
cal
Inher
it
ance,
onecl
assser
vesasasuper
class(
basecl
ass)f
ormor
ethanone
subcl
ass.
Inbel
owi
mage,
thecl
assAser
vesasabasecl
assf
ort
heder
ivedcl
assB,
CandD.

/
/Jav
apr
ogr
am t
oil
l
ust
rat
ethe

/
/conceptofHi
erar
chi
cal
inher
it
ance

i
mpor
tjav
a.i
o.*
;

cl
assone

publ
i
cvoi
dpr
int
_geek(
)

Sy
stem.
out
.pr
int
ln(
"Geeks"
);

}
cl
asst
woext
endsone

publ
i
cvoi
dpr
int
_oop(
)

Sy
stem.
out
.pr
int
ln(
"OOP"
);

cl
asst
hreeext
endsone

publ
i
cvoi
dpr
int
_jav
a()

Sy
stem.
out
.pr
int
ln(
"j
ava"
);

/
/Dr
ivedcl
ass

publ
i
ccl
assA

publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs)

{
t
hreeg=newt
hree(
);

g.
pri
nt_
geek(
);

g.
pri
nt_
jav
a()
;

t
wot=newt
wo(
);

t
.pr
int
_geek(
);

t
.pr
int
_oop(
);

Out
put
:

Geeks

JAVA

Geeks

OOP

Mul
ti
pleI
nher
it
ance

(
Thr
oughI
nter
faces):
InMul
ti
plei
nher
it
ance,
onecl
asscanhav
emor
ethanonesuper
classand
i
nher
itf
eat
uresf
rom al
lpar
entcl
asses.Pl
easenot
ethatJav
adoesnotsuppor
tmul
ti
ple
i
nher
it
ancewi
thcl
asses.I
njav
a,wecanachi
evemul
ti
plei
nher
it
anceonl
ythr
oughI
nter
faces.I
n
i
magebel
ow,
ClassCi
sder
ivedf
rom i
nter
faceAandB.
/
/Jav
apr
ogr
am t
oil
l
ust
rat
ethe

/
/conceptofMul
ti
plei
nher
it
ance

i
mpor
tjav
a.i
o.*
;

i
nter
faceone

publ
i
cvoi
dpr
int
_geek(
);

i
nter
facet
wo

publ
i
cvoi
dpr
int
_for
();

i
nter
facet
hreeext
endsone,
two
{

publ
i
cvoi
dpr
int
_geek(
);

cl
asschi
l
dimpl
ement
sthr
ee

@Ov
err
ide

publ
i
cvoi
dpr
int
_geek(
){

Sy
stem.
out
.pr
int
ln(
"Geeks"
);

publ
i
cvoi
dpr
int
_for
()

Sy
stem.
out
.pr
int
ln(
"for
");

/
/Dr
ivedcl
ass

publ
i
ccl
assP

publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs)

{
chi
l
dc=newchi
l
d()
;

c.
pri
nt_
geek(
);

c.
pri
nt_
for
();

c.
pri
nt_
geek(
);

Out
put
:

Geeks

f
or

Geeks

i
mpor
tjav
a.i
o.*
;

/
/Jav
apr
ogr
am t
oil
l
ust
rat
ethe

/
/conceptofi
nher
it
ance

/
/basecl
ass

cl
assBi
cycl
e
{

/
/theBi
cycl
ecl
asshast
wof
iel
ds

publ
i
cintgear
;

publ
i
cintspeed;

/
/theBi
cycl
ecl
asshasoneconst
ruct
or

publ
i
cBi
cycl
e(i
ntgear
,intspeed)

t
his.
gear=gear
;

t
his.
speed=speed;

/
/theBi
cycl
ecl
asshast
hreemet
hods

publ
i
cvoi
dappl
yBr
ake(
intdecr
ement
)

speed-
=decr
ement
;

publ
i
cvoi
dspeedUp(
inti
ncr
ement
)

speed+=i
ncr
ement
;
}

/
/toSt
ri
ng(
)met
hodt
opr
inti
nfoofBi
cycl
e

publ
i
cSt
ri
ngt
oSt
ri
ng(
)

r
etur
n("
Noofgear
sar
e"+gear

+"
\n"

+"
speedofbi
cycl
eis"
+speed)
;

/
/der
ivedcl
ass

cl
assMount
ainBi
keext
endsBi
cycl
e

/
/theMount
ainBi
kesubcl
assaddsonemor
efi
eld

publ
i
cintseat
Hei
ght
;

/
/theMount
ainBi
kesubcl
asshasoneconst
ruct
or

publ
i
cMount
ainBi
ke(
intgear
,i
ntspeed,

i
ntst
art
Hei
ght
)
{

/
/inv
oki
ngbase-
class(
Bicy
cle)const
ruct
or

super
(gear
,speed)
;

seat
Hei
ght=st
art
Hei
ght
;

/
/theMount
ainBi
kesubcl
assaddsonemor
emet
hod

publ
i
cvoi
dset
Hei
ght
(i
ntnewVal
ue)

seat
Hei
ght=newVal
ue;

/
/ov
err
idi
ngt
oSt
ri
ng(
)met
hod

/
/ofBi
cycl
etopr
intmor
einf
o

@Ov
err
ide

publ
i
cSt
ri
ngt
oSt
ri
ng(
)

r
etur
n(super
.t
oSt
ri
ng(
)+

"
\nseathei
ghti
s"+seat
Hei
ght
);

}
}

/
/dr
ivercl
ass

publ
i
ccl
assTest

publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])

Mount
ainBi
kemb=newMount
ainBi
ke(
3,100,
25)
;

Sy
stem.
out
.pr
int
ln(
mb.
toSt
ri
ng(
));

i
mpor
tjav
a.i
o.*
;

/
/BaseCl
assVehi
cl
e
cl
assVehi
cle{

/
/Pr
ivat
eFi
elds

pr
ivat
eSt
ri
ngmake;

pr
ivat
eSt
ri
ngcol
or;

pr
ivat
einty
ear
;

pr
ivat
eSt
ri
ngmodel
;

/
/Par
amet
eri
zedConst
ruct
or

publ
i
cVehi
cle(
Str
ingmake,
Str
ingcol
or,
inty
ear
,St
ri
ngmodel
){

t
his.
make=make;

t
his.
col
or=col
or;

t
his.
year=y
ear
;

t
his.
model
=model
;

/
/publ
i
cmet
hodt
opr
intdet
ail
s

publ
i
cvoi
dpr
int
Det
ail
s(){

Sy
stem.
out
.pr
int
ln(
"Manuf
act
urer
:"+make)
;

Sy
stem.
out
.pr
int
ln(
"Col
or:
"+col
or)
;
Sy
stem.
out
.pr
int
ln(
"Year
:"+y
ear
);

Sy
stem.
out
.pr
int
ln(
"Model
:"+model
);

/
/Der
ivedCl
assCar

cl
assCarext
endsVehi
cl
e{

/
/Pr
ivat
efi
eld

pr
ivat
eSt
ri
ngbody
Sty
le;

/
/Par
amet
eri
zedConst
ruct
or

publ
i
cCar
(St
ri
ngmake,
Str
ingcol
or,
inty
ear
,St
ri
ngmodel
,St
ri
ngbody
Sty
le){

super
(make,
col
or,
year
,model
);/
/cal
l
ingpar
entcl
assconst
ruct
or

t
his.
body
Sty
le=body
Sty
le;

publ
i
cvoi
dcar
Det
ail
s(){/
/det
ail
sofcar

pr
int
Det
ail
s()
; /
/cal
l
ingmet
hodf
rom par
entcl
ass

Sy
stem.
out
.pr
int
ln(
"BodySt
yle:
"+body
Sty
le)
;

}
}

cl
assMai
n{

publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

Carel
ant
raSedan=newCar
("Hy
undai
","
Red"
,2019,
"El
ant
ra"
,"Sedan"
);/
/cr
eat
ionof
carObj
ect

el
ant
raSedan.
car
Det
ail
s()
;//
cal
l
ingmet
hodt
opr
intdet
ail
s

Met
hodOv
err
idi
ngi
nJav
aInher
it
ance

I
nExampl
e1,
weseet
heobj
ectoft
hesubcl
asscanaccesst
hemet
hodoft
hesuper
class.
Howev
er,
ift
hesamemet
hodi
spr
esenti
nbot
hthesuper
classandsubcl
ass,
whatwi
l
lhappen?
I
nthi
scase,
themet
hodi
nthesubcl
assov
err
idest
hemet
hodi
nthesuper
class.Thi
sconcepti
s
knownasmet
hodov
err
idi
ngi
nJav
a.

Exampl
e2:
Met
hodov
err
idi
ngi
nJav
aInher
it
ance

cl
assAni
mal
{
/
/met
hodi
nthesuper
class

publ
i
cvoi
deat
(){

Sy
stem.
out
.pr
int
ln(
"Icaneat
");

/
/Dogi
nher
it
sAni
mal

cl
assDogext
endsAni
mal
{

/
/ov
err
idi
ngt
heeat
()met
hod

@Ov
err
ide

publ
i
cvoi
deat
(){

Sy
stem.
out
.pr
int
ln(
"Ieatdogf
ood"
);

/
/newmet
hodi
nsubcl
ass

publ
i
cvoi
dbar
k(){

Sy
stem.
out
.pr
int
ln(
"Icanbar
k")
;

}
cl
assMai
n{

publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

/
/cr
eat
eanobj
ectoft
hesubcl
ass

Dogl
abr
ador=newDog(
);

/
/cal
ltheeat
()met
hod

l
abr
ador
.eat
();

l
abr
ador
.bar
k()
;

Out
put

Ieatdogf
ood

Icanbar
k

I
ntheabov
eexampl
e,t
heeat
()met
hodi
spr
esenti
nbot
hthesuper
classAni
malandt
he
assDog.
subcl
Her
e,wehav
ecr
eat ectl
edanobj abr
adorofDog.
Nowwhenwecal
leat
()usi
ngt ectl
heobj abr
ador
,themet
hodi deDogi
nsi scal
l
ed.Thi
sis
becauset
hemet
hodi
nsi
det
heder
ivedcl
assov
err
idest
hemet
hodi
nsi
det
hebasecl
ass.
Thi
siscal
l
edmet
hodov
err
idi
ng.Tol
ear
nmor
e,v
isi
tJav
aMet
hodOv
err
idi
ng.
Not
e:Wehav
eusedt
he@Ov
err
ideannot
ati
ont
otel
lthecompi
l
ert
hatwear
eov
err
idi
nga
met
hod.Howev
er,
theannot
ati
oni
snotmandat
ory
.Tol
ear
nmor
e,v
isi
tJav
aAnnot
ati
ons.
superKey
wor
dinJav
aInher
it
ance

Pr
evi
ousl
ywesawt
hatt
hesamemet
hodi
nthesubcl
assov
err
idest
hemet
hodi
nsuper
class.
I
nsuchasi
tuat
ion,
thesuperkey
wor
disusedt
ocal
lthemet
hodoft
hepar
entcl
assf
rom t
he
met
hodoft
hechi
l
dcl
ass.

Exampl
e3:
superKey
wor
dinI
nher
it
ance

cl
assAni
mal
{

/
/met
hodi
nthesuper
class

publ
i
cvoi
deat
(){

Sy
stem.
out
.pr
int
ln(
"Icaneat
");

/
/Dogi
nher
it
sAni
mal

cl
assDogext
endsAni
mal
{

/
/ov
err
idi
ngt
heeat
()met
hod

@Ov
err
ide

publ
i
cvoi
deat
(){

/
/cal
lmet
hodofsuper
class

super
.eat
();
Sy
stem.
out
.pr
int
ln(
"Ieatdogf
ood"
);

/
/newmet
hodi
nsubcl
ass

publ
i
cvoi
dbar
k(){

Sy
stem.
out
.pr
int
ln(
"Icanbar
k")
;

cl
assMai
n{

publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

/
/cr
eat
eanobj
ectoft
hesubcl
ass

Dogl
abr
ador=newDog(
);

/
/cal
ltheeat
()met
hod

l
abr
ador
.eat
();

l
abr
ador
.bar
k()
;

Out
put
Icaneat

Ieatdogf
ood

Icanbar
k

pr
otect
edMember
sinI
nher
it
ance

I
nJav
a,i
facl
assi
ncl
udespr
otect
edf
iel
dsandmet
hods,
thent
hesef
iel
dsandmet
hodsar
e
accessi
blef
rom t
hesubcl
assoft
hecl
ass.

Exampl
e4:
prot
ect
edMember
sinI
nher
it
ance

cl
assAni
mal
{

pr
otect
edSt
ri
ngname;

pr
otect
edv
oiddi
spl
ay(
){

Sy
stem.
out
.pr
int
ln(
"Iam anani
mal
."
);

cl
assDogext
endsAni
mal
{

publ
i
cvoi
dget
Inf
o(){

Sy
stem.
out
.pr
int
ln(
"Mynamei
s"+name)
;

}
}

cl
assMai
n{

publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

/
/cr
eat
eanobj
ectoft
hesubcl
ass

Dogl
abr
ador=newDog(
);

/
/accesspr
otect
edf
iel
dandmet
hod

/
/usi
ngt
heobj
ectofsubcl
ass

l
abr
ador
.name="
Rocky
";

l
abr
ador
.di
spl
ay(
);

l
abr
ador
.get
Inf
o()
;

Out
put

Iam anani
mal
.

Mynamei
sRocky

You might also like