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

Comput

erAppl
icat
ions-X

01-
04-
2021

Funct
ionOv
erl
oadi
ng

Afuncti
onnamehavingseveraldef
ini
ti
onsinthesamescopethataredi
ff
eret
iabl
ebythe
numberorty
peoftheirar
gument si
ssaidtobeanov er
loadedf
uncti
on.Pr
ocessofcr
eati
ng
over
loadedf
unct
ionsiscal
ledfuncti
onoverl
oading.

I
faclasshasmult
ipl
emet
hodshav
ingsamenamebutdi
ff
erentpar
amet
ers,
iti
sknownas
Met
hodOv er
loadi
ng.

 Funct
ionov
erl
oadi
ngi
mpl
ement
sPol
ymor
phi
sm.

Adv
ant
ageofmet
hodov
erl
oadi
ng

 Methodoverl
oadi
ngincr
easesther
eadabil
i
tyoftheprogram.
 I
treducesnumberofcompari
sonsi
napr ogram andt
herebymakest
hepr
ogr
am r
un
f
aster

Decl
arat
ionanddef
ini
ti
on

Thekeytofuncti
onov
erl
oadi
ngi
saf
unct
ion'
sar
gumentl
i
stwhi
chi
sal
soknownast
he
funct
ionsi
gnatur
e.

Ift
wofunct
ionsarehav
ingsamenumberandtypesofar
gumentsi
nthesameor
der
,theyar
e
sai
dtohavethesamesignat
ure.Ev
eni
ftheyar
eusingdi
sti
nctv
ari
abl
enames.

v
oidsqr
(inta,
floatb) //f
unct
ion1
v
oidsqr
(intx,
floaty) /
/samesignat
ureast
hatoff
unct
ion1

Whenafuncti
onnameisdeclaredmorethanoncei
napr
ogr
am,
thecompi
l
erwi
l
lint
erpr
ett
he
second(
subsequent
)decl
arat
ionasfol
lows

 Ifthesi
gnaturesofsubsequentf uncti
onsmat cht hepreviousfunct
ion'
ssignatur
e,t
hen
thesecondist r
eatedasr e-
declarati
onoft hef i
rstandflaggedatcompi l
et i
measan
error
.
 Ifthesi
gnaturesoft wofuncti
onsmat chbutt heretur
nt ypediff
ers,t
henthesecondis
treat
edasre-declarati
onofthef i
rstandflaggedatcompi letimeasaner ror.
 Ifthesi
gnaturesoft wofuncti
onsdi f
ferineitherthenumberort ypeoftheirar
guments,
thetwofuncti
onsar econsideredt obeov erl
oaded.

Di
ff
erentway
stoov
erl
oadt
hemet
hod

Ther
ear
etwoway
stoov
erl
oadt
hemet
hodi
njav
a
 Bychangi
ngnumberofar
gument
s
 Bychangi
ngthedat
atype

I
nJav
a,Met
hodOv
erl
oadi
ngi
snotpossi
blebychangi
ngt
her
etur
nty
peoft
hemet
hodonl
y.

1)MethodOverl
oadi
ng:changingno.ofar guments
I
nthisexample,
wehavecr eat
edtwomet hods,fi
rstadd(
)met hodperf
ormsaddi
ti
onoftwo
numbersandsecondaddmet hodperformsaddi t
ionofthreenumbers.Wearecr
eati
ngstat
ic
methodssothatwedon'tneedtocreateinstanceforcal
li
ngmet hods.

classAdder{
stati
cintadd(
inta,
i
ntb)
{
retur
na+b;
}
stati
cintadd(
inta,
i
ntb,
i
ntc)
{
retur
na+b+c;
}
}

cl
assTestOverloadi
ng1
{
publ
icst
ati
cv oidmai n(
Str
ing[
]ar
gs)
{
Syst
em.out.
printl
n(Adder.
add(
11,
11)
);
Syst
em.out.
printl
n(Adder.
add(
11,
11,
11)
);
}
}

Output
:
22
33

2)Met hodOverl
oading:changi
ngdataty
peofarguments
Inthi
sexample, wehavecreatedtwomethodst
hatdif
fer
sindatat
ype.Thef
ir
staddmethod
recei
vestwointegerargumentsandsecondaddmethodrecei
vest
wodoubleargument
s.
classAdder
{
stati
cintadd(
inta,i
ntb)
{
retur
na+b;
}
stati
cdoubleadd(doublea,doubl
eb)
{
r
etur
na+b;
}
}

cl
assTestOverloadi
ng2
{
publ
icst
ati
cv oidmai n(
Str
ing[
]ar
gs)
{
Syst
em.out.
printl
n(Adder.
add(
11,
11));
Syst
em.out.
printl
n(Adder.
add(
12.
3,12.
6))
;
}}

Output
:
22
24.
9

QWhyMet hodOv erloadingi


snotpossi
blebychangingthereturnt
ypeofmethodonl
y?
Injava,methodoverloadi
ngisnotpossi
blebychangi
ngthereturnty
peofthemethodonl
y
becauseofambi guity.
classAdder
{
stati
cintadd(i
nta,
intb)
{
retur
na+b;
}
stati
cdoubleadd(inta,i
ntb)
{
retur
na+b;
}}

cl
assTestOverloadi
ng3{
publ
icst
ati
cv oidmai n(
Str
ing[
]ar
gs)
{
Syst
em.out.
printl
n(Adder.
add(
11,
11)
);/
/ambi
gui
ty

}
}
Out
put
:

Compil
eTimeError
:methodadd(
int
,i
nt)i
salr
eadydef
inedincl
assAdder
Syst
em.out
.pr
int
ln(
Adder
.add(
11,
11))
;//Her
e,howcanjavadet
ermi
newhichsum(
)met
hod
shoul
dbecall
ed?

QCanweov
erl
oadj
avamai
n()met
hod?

Yes,bymethodover
loadi
ng.Wecanhaveanynumberofmainmet
hodsinacl
assbymet
hod
over
loadi
ng.ButJVM cal
l
smain(
)methodwhichrecei
vesst
ri
ngar
rayasar
gument
sonl
y.
cl
assTestOverloadi
ng4
{
publ
icst
ati
cv oidmain(
Str
ing[
]args)
{
Syst
em.out.
printl
n("
mainwit
hStri
ng[]
");
}
publ
icst
ati
cv oidmain(
Str
ingargs)
{
Syst
em.out.
printl
n("
mainwit
hStri
ng");
}
publ
icst
ati
cv oidmain(
)
{
Syst
em.out.
printl
n("
mainwit
houtargs"
);
}
}

Out
put
:
mai
nwithSt
ri
ng[
]

Met
hodOv
erl
oadi
ngandTy
pePr
omot
ion

Onet
ypei
spr
omot
edt
oanot
heri
mpl
i
cit
lyi
fnomat
chi
ngdat
aty
pei
sfound.

Jav
aMet
hodOv
erl
oadi
ngwi
thTy
pePr
omot
ion

byt
ecanbepr omotedtoshor
t,i
nt,l
ong,fl
oatordouble.Theshortdatat
ypecanbepromotedt
o
i
nt,l
ong,
floatordoubl
e.Thechardatat
ypecanbepr omotedtoint,
long,
fl
oatordoubl
eandso
on.

Not
e:Whencal
l
ingf
unct
ions,
useconst
antsuf
fi
xest
oav
oidambi
gui
ty.

Pr
ojectFi
le

1.Desi
gnaclasstoover
loadaf
unct
ionvol
ume()asfol
l
ows:
i
)doublevol
ume(doubl
er)—wit
hradius(
r)asanar
gument
,ret
urnst
hev
olumeofspher
eusi
ng
thef
ormula:
V=( 4/
3)*(22/
7)*r*r*r

i
i)doublevol
ume(doubl
eh,doubl
er )—wit
hhei
ght
(h)andr
adi
us(
r)ast
hear
gument
s,r
etur
ns
t
hev olumeofacyli
nderusi
ngtheformul
a:
V=( 22/7)*r*r*h

i
ii
)doublevolume(double1,doubl
eb,doubl
eh)—wit
hlengt
h(l
),br
eadt
h(b)andhei
ght
(h)ast
he
ar
gument s,
retur
nsthev ol
umeofacuboi dusi
ngt
hefor
mula:
V=l*b*h⇒ (lengt
h*br eadt
h*height
)
2.Designaclasstoover
loadaf uncti
onseri
es()asfol
l
ows:
a)voidser
ies(i
ntx,i
ntn)
:todisplaythesum oftheser
iesgi
venbel
ow:
s=( x/
1)–( x/
2)+(x/3)–(x/4)+( x/
5)…t onterms

b)voidseri
es(
intp):
todi
spl
ayt
hef
oll
owi
ngser
ies:
0,7,26,63,…pterms.

c)voi
dser
ies(
):t
odi
spl
ayt
hesum oft
heser
iesgi
venbel
ow:
1/2+1/3+1/4+…+1/10.

Assi
gnmentcopy

1.Desi gnaclasstoover
loadafunct
ionpoly
gon()asfol
lows:
(i)voidpoly
gon(i
ntn,charch)
:wit
honeintegerargumentandonecharacter
ar gumentthatdrawsafil
l
edsquareofsi
denusi ngthecharact
erst
oredinch.
Inputv al
ueofn=2, ch=‘O’
Out put:
OO
OO

(
ii
)voidpol
ygon(i
ntx,
inty
):withtwointegerar
gument
sthatdr
awsaf
il
led
r
ectangl
eoflengt
hxandbr eadt
hy ,
usingthesymbol‘
@’.
I
nputval
ueofx=2, y=5
Out
put:
@@@@@
@@@@@

(
ii
i)voi
dpol
ygon(
):wi
thnoar
gument
sthatdr
awsaf
il
ledt
ri
angl
eshownbel
ow.
Output
:
*
**
**
*
**
**
**
***

2.Designaclasstoov erloadafunct
ionSumSeries(
)asfol
l
ows:
(i
)voidSumSer i
es(i
ntn,doublex)–wi thoneint
egerar
gumentandonedoubl
ear
gument
tofindanddisplaythesum oft heseri
esgiv
enbelow:
4 5
s=(x¹)–(x²)+(x³)–( x)+( x)…t ont er
ms

(
ii
)voi
dSumSeri
es(
)–Tofi
ndanddi
spl
ayt
hesum oft
hef
oll
owi
ngser
ies:
s=1+( 1X2)+(1X2X3)+…+(1X2X3X4X…20)

You might also like