13Java super-WPS Office
13Java super-WPS Office
asuper
I
nthi
stut
ori
al,
wewi
l
llear
naboutt
hesuperkey
wor
dinJav
awi
tht
hehel
pofexampl
es.
Thesuperkeywor
dinJavaisusedi
nsubcl
assest
oaccesssuper
classmember
s(at
tri
but
es,
const
ruct
orsandmethods)
.
Bef
orewel
ear
naboutt
hesuperkey
wor
d,makesur
etoknowaboutJav
ainher
it
ance.
Usesofsuperkey
wor
d
Tocal
lmet
hodsoft
hesuper
classt
hati
sov
err
iddeni
nthesubcl
ass.
Toaccessat
tri
but
es(
fi
elds)oft
hesuper
classi
fbot
hsuper
classandsubcl
asshav
eat
tri
but
es
wi
ththesamename.
Toexpli
cit
lycal
lsuper
classno-
arg(
def
aul
t)orpar
amet
eri
zedconst
ruct
orf
rom t
hesubcl
ass
const
ruct
or.
Let
’sunder
standeachoft
heseuses.
1.AccessOv
err
iddenMet
hodsoft
hesuper
class
Ifmet
hodswit
hthesamenamearedefi
nedinbothsuper
classandsubcl
ass,themethodi
nthe
subcl
assov
err
idest
hemethodi
nthesuper
class.Thi
siscall
edmethodoverr
iding.
Exampl
e1:
Met
hodov
err
idi
ng
cl
assAni
mal
{
/
/ov
err
iddenmet
hod
publ
i
cvoi
ddi
spl
ay(
){
Sy
stem.
out
.pr
int
ln(
"Iam anani
mal
")
;
}
cl
assDogext
endsAni
mal
{
/
/ov
err
idi
ngmet
hod
@Ov
err
ide
publ
i
cvoi
ddi
spl
ay(
){
Sy
stem.
out
.pr
int
ln(
"Iam adog"
);
publ
i
cvoi
dpr
int
Message(
){
di
spl
ay(
);
cl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Dogdog1=newDog(
);
dog1.
pri
ntMessage(
);
RunCode
Out
put
Iam adog
I
nthi
sexample,bymakinganobjectdog1ofDogcl
ass,
wecancal
li
tsmet
hodpr
int
Message(
)
whi
chthenexecut
esthedispl
ay(
)statement
.
Si
ncedisplay
()i
sdef
inedinbotht
hecl
asses,
themethodofsubcl
assDogov
err
idest
hemet
hod
ofsuper
classAni
mal.Hence,
thedi
spl
ay(
)ofthesubcl
assi
scall
ed.
Jav
aov
err
idi
ngexampl
e
Whati
ftheov
err
iddenmet
hodoft
hesuper
classhast
obecal
l
ed?
Weusesuper
.di
spl
ay(
)ift
heov
err
iddenmet
hoddi
spl
ay(
)ofsuper
classAni
mal
needst
obe
cal
l
ed.
Exampl
e2:
supert
oCal
lSuper
classMet
hod
cl
assAni
mal
{
/
/ov
err
iddenmet
hod
publ
i
cvoi
ddi
spl
ay(
){
Sy
stem.
out
.pr
int
ln(
"Iam anani
mal
")
;
cl
assDogext
endsAni
mal
{
/
/ov
err
idi
ngmet
hod
@Ov
err
ide
publ
i
cvoi
ddi
spl
ay(
){
Sy
stem.
out
.pr
int
ln(
"Iam adog"
);
publ
i
cvoi
dpr
int
Message(
){
/
/thi
scal
l
sov
err
idi
ngmet
hod
di
spl
ay(
);
/
/thi
scal
l
sov
err
iddenmet
hod
super
.di
spl
ay(
);
cl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Dogdog1=newDog(
);
dog1.
pri
ntMessage(
);
RunCode
Out
put
Iam adog
Iam anani
mal
Her
e,howt
heabov
epr
ogr
am wor
ks.
Wor
kingofsuperi
nJav
a
2.AccessAt
tri
but
esoft
heSuper
class
Thesuper
classandsubcl
asscanhaveat
tri
buteswit
hthesamename.Weuset
hesuper
key
wordtoaccesstheatt
ri
buteoft
hesupercl
ass.
Exampl
e3:
Accesssuper
classat
tri
but
e
cl
assAni
mal
{
pr
otect
edSt
ri
ngt
ype="
ani
mal
";
cl
assDogext
endsAni
mal
{
publ
i
cSt
ri
ngt
ype="
mammal
";
publ
i
cvoi
dpr
int
Type(
){
Sy
stem.
out
.pr
int
ln(
"Iam a"+t
ype)
;
Sy
stem.
out
.pr
int
ln(
"Iam an"+super
.t
ype)
;
cl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Dogdog1=newDog(
);
dog1.
pri
ntTy
pe(
);
}
RunCode
Out
put
:
Iam amammal
Iam anani
mal
I
nthi
sexample,
wehav
edef
inedt
hesamei
nst
ancef
iel
dty
pei
nbot
hthesuper
classAni
mal
and
t
hesubcl
assDog.
Wethencr
eat
edanobj
ectdog1oft
heDogcl
ass.Then,
thepr
int
Type(
)met
hodi
scal
l
edusi
ng
t
hisobj
ect
.
I
nsi
det
hepr
int
Type(
)funct
ion,
t
yper
efer
stot
heat
tri
but
eoft
hesubcl
assDog.
super
.t
yper
efer
stot
heat
tr
ibut
eoft
hesuper
classAni
mal
.
Hence,
Syst
em.out.
printl
n("
Iam a"+ty
pe);
pri
ntsIam amammal
.And,
Syst
em.
out
.pr
int
ln(
"Iam
an"+super
.t
ype);
printsIam anani
mal.
3.Useofsuper
()t
oaccesssuper
classconst
ruct
or
Asweknow,
whenanobj
ectofacl
assi
scr
eat
ed,
itsdef
aul
tconst
ruct
ori
saut
omat
ical
l
ycal
l
ed.
Toexpli
cit
lycal
lthesuper
classconst
ruct
orf
rom t
hesubcl
assconst
ruct
or,
weusesuper
().I
t'
sa
speci
alf
orm ofthesuperkeyword.
super
()canbeusedonl
yinsi
det
hesubcl
assconst
ruct
orandmustbet
hef
ir
stst
atement
.
Exampl
e4:
Useofsuper
()
cl
assAni
mal
{
/
/def
aul
torno-
argconst
ruct
orofcl
assAni
mal
Ani
mal
(){
Sy
stem.
out
.pr
int
ln(
"Iam anani
mal
")
;
cl
assDogext
endsAni
mal
{
/
/def
aul
torno-
argconst
ruct
orofcl
assDog
Dog(
){
/
/cal
l
ingdef
aul
tconst
ruct
oroft
hesuper
class
super
();
Sy
stem.
out
.pr
int
ln(
"Iam adog"
);
cl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Dogdog1=newDog(
);
}
RunCode
Out
put
Iam anani
mal
Iam adog
Here,
whenanobjectdog1ofDogcl
assi
scr
eat
ed,
itaut
omat
ical
l
ycal
l
sthedef
aul
torno-
arg
const
ruct
oroft
hatclass.
Insi
dethesubclassconst
ruct
or,t
hesuper
()st
atementcal
l
stheconstruct
oroft
hesuper
class
andexecutesthestat
ementsinsi
dei
t.Hence,
wegettheoutputIam ananimal
.
Wor
kingofsuper
()i
nJav
a
Thefl
owoftheprogr
am thenret
urnsbackt
othesubcl
assconst
ruct
orandexecut
est
he
remai
ningst
atement
s.Thus,Iam adogwil
lbepr
int
ed.
However,
usingsuper
()isnotcompulsory
.Evenifsuper
()i
snotusedinthesubclass
const
ruct
or,
thecompileri
mplici
tl
ycall
sthedefaul
tconstr
uct
orofthesuper
class.
So,
whyuser
edundantcodei
fthecompi
l
eraut
omat
ical
l
yinv
okessuper
()?
Iti
srequir
edift
heparameter
izedconst
ructor(
aconstruct
ort
hatt
akesar
gument
s)oft
he
supercl
asshastobecall
edfr
om thesubclassconst
ructor
.
Theparameteri
zedsuper
()mustal
way
sbet hef
ir
ststat
ementi
nthebodyoft
heconst
ruct
orof
thesubcl
ass,ot
herwi
se,wegetacompil
ati
oner
ror.
Exampl
e5:
Cal
lPar
amet
eri
zedConst
ruct
orUsi
ngsuper
()
cl
assAni
mal
{
/
/def
aul
torno-
argconst
ruct
or
Ani
mal
(){
Sy
stem.
out
.pr
int
ln(
"Iam anani
mal
")
;
/
/par
amet
eri
zedconst
ruct
or
Ani
mal
(St
ri
ngt
ype){
Sy
stem.
out
.pr
int
ln(
"Ty
pe:
"+t
ype)
;
cl
assDogext
endsAni
mal
{
/
/def
aul
tconst
ruct
or
Dog(
){
/
/cal
l
ingpar
amet
eri
zedconst
ruct
oroft
hesuper
class
super
("Ani
mal
")
;
Sy
stem.
out
.pr
int
ln(
"Iam adog"
);
}
}
cl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Dogdog1=newDog(
);
RunCode
Out
put
Ty
pe:
Ani
mal
Iam adog
Thecompi
lercanautomati
cal
lycal
ltheno-
argconst
ruct
or.Howev
er,
itcannotcal
l
par
ameter
izedconst
ruct
ors.
Ifaparameter
izedconst
ruct
orhast
obecal
l
ed,
weneedt
oexpl
i
cit
lydef
inei
tint
hesubcl
ass
constr
uctor
.
Wor
kingofsuperi
ncaseofpar
amet
eri
zedconst
ruct
or.
Notethati
ntheaboveexample,
weexpli
cit
lycal
ledtheparameteri
zedconst
ruct
or
super
("Ani
mal"
).Thecompil
erdoesnotcal
lthedefaul
tconstr
uctoroft
hesupercl
assi
nthi
s
case.