You are on page 1of 5

Const

ruct
ors-2:

The1stli
neinsi
deev
er yconst
ructorshouldbeeit
hersuper
()ort
his(
).I
fwedon’
twr
it
e
anyt
hingcompil
erwi
llalwaysgeneratesuper(
).

Variouswaystocal
ltheconst
ruct
or:
a)Testtest=newTest(
);
b)newTest (
);
c)this(
);
d)super()
;

Not e:
this()andsuper(
)canbeusedi nconst
ruct
orsonly
.Ifwewrit
eitatanyot
her
place,itwil
lbecompileti
meer ror.
Const ruct
ordoesn’
tinher
itandhenceconstruct
oroverr
idi
ngisnotpossi
ble.
Const ruct
orscanonlybeov er
loaded.

t
his(
)-
t
his(
)isusedt
ocal
lthecur
rentcl
assconst
ruct
or.

Const
ruct
orchai
ning-

Const
ruct
orchai
ningistheprocessofcalli
ngoneconst
ruct
orf
rom anot
her
const
ruct
orwit
hrespecttocurrentobject

Const
ruct
orchai
ningcanbedonei ntwoway s:
 
 

Wit
hinsamecl
ass:I
tcanbedoneusi
ng 
thi
s()
 key
wor
dforconst
ruct
orsi
nsame
cl
ass

Fr
om basecl
ass:
 byusi
ng 
super
() 
key
wor
dtocal
lconst
ruct
orf
rom t
hebase
cl
ass.

#Pr
ogr
am-
1.1t
ouseofcur
rentcl
assconst
ruct
or-

packagecom.
test
;
cl
assTest{

publ
icTest
(){
Syst
em.out
.pr
int
ln(
"Testconst
ruct
orcal
l
edusi
ng"+"t
hiskey
wor
d..
")
;
}

publ
icTest(
intx){
thi
s()
; /
/inv
oket
heconst
ruct
orofcur
rentcl
ass
}

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

Testt
est=newTest
(10)
;
}

}
Out
put
-

Testconst
ruct
orcal
l
edusi
ngt
hiskey
wor
d..

#Pr
ogr
am-
1.2t
ouseofcur
rentcl
assconst
ruct
or-

publ
iccl
assExampl
e{

Exampl
e(){
thi
s("Java"
);
System.out.
pri
ntl
n("
Insi
deConst
ruct
orwi
thoutpar
amet
er"
);
}

Exampl
e(Str
ingst
r){
Syst
em.out.
pri
ntl
n("
Insi
deConst
ruct
orwi
thpar
amet
er"+st
r);
}

publ
icstat
icv
oidmain(
Str
ing[
]ar
gs){
Exampleobj
=newExample()
;
}
}

Out
put
-

I
nsi
deConst
ruct
orwi
thStri
ngpar
ameterasJav
a
I
nsi
deConst
ruct
orwi
thoutpar
ameter
super
()–
Iti
susedtocal
lthei
mmedi
atepar
entcl
assconst
ruct
or

#Pr
ogr
am-
3touseofi
mmedi
atesupercl
assconst
ruct
or-

cl
assPar
ent{

Par
ent
(){
Syst out
em. .pr
int
ln(
"Par
entcl
assconst
ruct
or.
")
;
}
}

cl
assChi
l
dext
endsPar
ent{

Chi
l
d(){
super
();
}
}

publ
iccl
assTest
Mai
n{

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

Chi
l
dc=newChi
l
d()
;

}
}
Out
put
-

Par
entcl
assconst
ruct
or.

Not
e:

1)I
fwewanttowri
tesuper
()ort
his()i
naconstr
uctorthenithastobefi
rst
l
i
neofconstr
uct
or.I
fwearewrit
ingitany
whereelsethenitwil
lbecompil
e
t
imeer
ror.
2)Wecan’tusesuper(
)orthi
s()atthesamet
imei
nsameconst
ruct
or.We
canei
therusesuper(
)orthi
s().
3)I
fwedon’
twrit
eanysuper(
)orthi
s()
,thencompi
l
erwi
l
lal
way
spl
acesuper
()
bydef
aul
tinf
ir
stl
ineofconstr
uctor
.

Summar
y:

Pr
ogr
ammerCode Compi
lerCode

cl
assDemo{ cl
assDemo{
Demo()
{
super
();
}
} }
publ
i
ccl
assDemo{ publi
cclassDemo{
publicDemo()
{
super(
);
}
} }

cl
assDemo{ cl
assDemo{
voidDemo(
){ voidDemo(
){

}
} Demo( ){
super(
);
} }
}
cl
assDemo{ cl
assDemo{
Demo(i
nti
){ Demo( i
nti)
{
super(
);
} }
} }
cl
assDemo{ cl
assDemo{
Demo( i
nti)
{ Demo( i
nti)
{
super(
); super(
);
} }
} }
cl
assDemo{ cl
assDemo{
Demo( i
nti)
{ Demo( i
nti)
{
thi
s()
; thi
s()
;
} }
Demo( )
{ Demo( )
{
Super();
} }
} }

You might also like