0% found this document useful (0 votes)
5 views9 pages

14java Abstract C-WPS Office

The document provides an overview of abstract classes and methods in Java, explaining their purpose and usage. It highlights that abstract classes cannot be instantiated and must be subclassed to access their members, while abstract methods require implementation in derived classes. Additionally, it discusses the concept of abstraction in object-oriented programming, emphasizing its role in managing complexity by hiding unnecessary details.

Uploaded by

David
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

14java Abstract C-WPS Office

The document provides an overview of abstract classes and methods in Java, explaining their purpose and usage. It highlights that abstract classes cannot be instantiated and must be subclassed to access their members, while abstract methods require implementation in derived classes. Additionally, it discusses the concept of abstraction in object-oriented programming, emphasizing its role in managing complexity by hiding unnecessary details.

Uploaded by

David
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Jav

aAbst
ractCl
ass

Jav
aAbst
ractCl
assandAbst
ractMet
hods

I
nthi
stut
ori
al,
wewi l
llear
naboutJavaabst
ractcl
assesandmet
hodswi
tht
hehel
pofexampl
es.
Wewil
lal
solearnaboutabst
ract
ioni
nJava.

Jav
aAbst
ractCl
ass

Theabst
ractcl
assinJavacannotbei
nstant
iated(wecannotcr
eateobject
sofabst
ract
cl
asses)
.Weuset heabst
ractkey
wordtodeclareanabstr
actcl
ass.Forexampl
e,

/
/cr
eat
eanabst
ractcl
ass

abst
ractcl
assLanguage{

/
/fi
eldsandmet
hods

.
..

/
/tr
ytocr
eat
eanobj
ectLanguage

/
/thr
owsaner
ror

Languageobj
=newLanguage(
);

Anabst
ractcl
asscanhav
ebot
hther
egul
armet
hodsandabst
ractmet
hods.Forexampl
e,

abst
ractcl
assLanguage{

/
/abst
ractmet
hod

abst
ractv
oidmet
hod1(
);
/
/regul
armet
hod

v
oidmet
hod2(
){

Sy
stem.
out
.pr
int
ln(
"Thi
sisr
egul
armet
hod"
);

Toknowaboutt
henon-
abst
ractmet
hods,
visi
tJav
amet
hods.Her
e,wewi
l
llear
naboutabst
ract
methods.

Jav
aAbst
ractMet
hod

Amethodthatdoesn'
thaveit
sbodyi sknownasanabst
ractmet
hod.Weuset
hesameabst
ract
key
wordtocreat
eabstractmethods.Forexampl
e,

abst
ractv
oiddi
spl
ay(
);

Her
e,di
spl
ay(
)isanabst
ractmet
hod.Thebodyofdi
spl
ay(
)isr
epl
acedby;
.

I
faclasscontai
nsanabstractmethod,
thent
hecl
assshoul
dbedecl
aredabst
ract
.Ot
her
wise,
it
wi
ll
generateanerr
or.Forexample,

/
/er
ror

/
/cl
assshoul
dbeabst
ract

cl
assLanguage{

/
/abst
ractmet
hod

abst
ractv
oidmet
hod1(
);

Exampl
e:Jav
aAbst
ractCl
assandMet
hod

Thoughabst
ractcl
assescannotbei
nst
ant
iat
ed,
wecancr
eat
esubcl
assesf
rom i
t.Wecant
hen
accessmember
soft
heabst
ractcl
assusi
ngt
heobj
ectoft
hesubcl
ass.Forexampl
e,

abst
ractcl
assLanguage{

/
/met
hodofabst
ractcl
ass

publ
i
cvoi
ddi
spl
ay(
){

Sy
stem.
out
.pr
int
ln(
"Thi
sisJav
aPr
ogr
ammi
ng"
);

cl
assMai
next
endsLanguage{

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

/
/cr
eat
eanobj
ectofMai
n

Mai
nobj
=newMai
n()
;

/
/accessmet
hodofabst
ractcl
ass

/
/usi
ngobj
ectofMai
ncl
ass

obj
.di
spl
ay(
);

RunCode

Out
put
Thi
sisJav
apr
ogr
ammi
ng

I
nt heaboveexample,
wehavecr
eat
edanabst
ractcl
assnamedLanguage.Thecl
asscont
ains
aregularmet
hoddispl
ay(
).

Wehav
ecr
eat
edt
heMai
ncl
asst
hati
nher
it
stheabst
ractcl
ass.Not
icet
hest
atement
,

obj
.di
spl
ay(
);

Here,
obji
stheobjectoft
hechi
l
dcl
assMai
n.Wear
ecal
l
ingt
hemet
hodoft
heabst
ractcl
ass
usi
ngtheobj
ectobj.

I
mpl
ement
ingAbst
ractMet
hods

Ift
heabstractcl
assi
ncl
udesanyabst
ractmethod,thenal
lthechi
ldcl
assesinher
it
edfrom t
he
abstr
actsupercl
assmustpr
ovi
detheimplementat
ionoftheabstr
actmethod.Forexampl
e,

abst
ractcl
assAni
mal
{

abst
ractv
oidmakeSound(
);

publ
i
cvoi
deat
(){

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

cl
assDogext
endsAni
mal
{

/
/pr
ovi
dei
mpl
ement
ati
onofabst
ractmet
hod

publ
i
cvoi
dmakeSound(
){

Sy
stem.
out
.pr
int
ln(
"Bar
kbar
k")
;
}

cl
assMai
n{

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

/
/cr
eat
eanobj
ectofDogcl
ass

Dogd1=newDog(
);

d1.
makeSound(
);

d1.
eat
();

RunCode

Out
put

Bar
kbar
k

Icaneat
.

I
ntheaboveexample,wehavecr
eatedanabstr
actcl
assAni
mal
.Thecl
asscont
ainsanabst
ract
methodmakeSound()andanon-
abstr
actmethodeat
().

Wehav
einher
itedasubcl
assDogfrom t
hesuper
classAnimal
.Her
e,t
hesubcl
assDogpr
ovi
des
t
hei
mplementati
onfort
heabst
ractmethodmakeSound()
.

Wet
henusedt
heobj
ectd1oft
heDogcl
asst
ocal
lmet
hodsmakeSound(
)andeat
().
Not
e:Ift
heDogcl assdoesn'
tpr
ovi
detheimpl
ementati
onoft
heabstr
actmethodmakeSound(
),
Dogshouldalsobedecl
aredasabst
ract
.Thi
sisbecauset
hesubcl
assDoginher
it
s
makeSound()fr
om Ani
mal .

AccessesConst
ruct
orofAbst
ractCl
asses

Anabstractcl
asscanhaveconst
ruct
orsli
ketheregul
arcl
ass.And,
wecanaccesst he
const
ructorofanabst
ractcl
assf
rom thesubcl
assusingt
hesuperkey
word.Forexample,

abst
ractcl
assAni
mal
{

Ani
mal
(){

….

cl
assDogext
endsAni
mal
{

Dog(
){

super
();

.
..

Her
e,wehav
eusedt
hesuper
()i
nsi
det
heconst
ruct
orofDogt
oaccesst
heconst
ruct
oroft
he
Ani
mal.

Notethatthesupershoul
dalway
sbet
hef
ir
stst
atementoft
hesubcl
assconst
ruct
or.Vi
sitJav
a
superkeywordtolear
nmor e.

Jav
aAbst
ract
ion
Themaj
oruseofabst
ractcl
assesandmet
hodsi
stoachi
eveabst
ract
ioni
nJav
a.

Abstr
actionisanimpor
tantconceptofobject
-or
ient
edpr
ogrammi
ngt
hatal
l
owsust
ohi
de
unnecessarydet
ail
sandonlyshowt heneededinfor
mati
on.

Thi
sall
owsust
omanagecompl
exi
tybyomi
tt
ingorhi
dingdet
ail
swi
thasi
mpl
er,
higher
-l
evel
i
dea.

Apract
icalexampleofabst
ract
ioncanbemot or
bikebrakes.Weknowwhatbrakedoes.When
weapplythebrake,t
hemotorbi
kewill
stop.Howev er
,theworki
ngoft
hebrakeiskepthi
dden
f
rom us.

Themaj
oradvant
ageofhi
dingtheworkingoft
hebr akei
sthatnowthemanufact
urercan
i
mplementbr
akedif
fer
ent
lyfordi
ff
erentmotor
bikes,however
,whatbr
akedoeswillbethesame.

Let
'st
akeanexampl
ethathel
psust
obet
terunder
standJav
aabst
ract
ion.

Exampl
e3:
Jav
aAbst
ract
ion

abst
ractcl
assMot
orBi
ke{

abst
ractv
oidbr
ake(
);

cl
assSpor
tsBi
keext
endsMot
orBi
ke{

/
/impl
ement
ati
onofabst
ractmet
hod

publ
i
cvoi
dbr
ake(
){

Sy
stem.
out
.pr
int
ln(
"Spor
tsBi
keBr
ake"
);
}

cl
assMount
ainBi
keext
endsMot
orBi
ke{

/
/impl
ement
ati
onofabst
ractmet
hod

publ
i
cvoi
dbr
ake(
){

Sy
stem.
out
.pr
int
ln(
"Mount
ainBi
keBr
ake"
);

cl
assMai
n{

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

Mount
ainBi
kem1=newMount
ainBi
ke(
);

m1.
brake(
);

Spor
tsBi
kes1=newSpor
tsBi
ke(
);

s1.
brake(
);

RunCode

Out
put
:

Mount
ainBi
keBr
ake

Spor
tsBi
keBr
ake

I
ntheaboveexample,
wehavecreat
edanabst
ractsupercl
assMot
orBi
ke.Thesuper
class
Motor
Bikehasanabstr
actmet
hodbrake(
).
Thebrake()methodcannotbeimplementedinsi
deMotor
Bike.I
tisbecauseev
erybi
kehas
di
ff
erentimplementati
onofbrakes.So,
allt
hesubcl
assesofMotorBikewoul
dhavedif
fer
ent
i
mplement at
ionofbrake(
).

So,
thei
mpl
ement
ati
onofbr
ake(
)inMot
orBi
kei
skepthi
dden.

Her
e,MountainBi
kemakesit
sowni
mpl
ement
ati
onofbr
ake(
)andSpor
tsBi
kemakesi
tsown
i
mplement
ationofbr
ake()
.

Note:Wecanal
sousei
nter
facest
oachi
eveabst
ract
ioni
nJav
a.Tol
ear
nmor
e,v
isi
tJav
a
I
nterf
ace.

KeyPoi
ntst
oRemember

Weuset
heabst
ractkey
wor
dtocr
eat
eabst
ractcl
assesandmet
hods.

Anabst
ractmet
hoddoesn'
thav
eanyi
mpl
ement
ati
on(
met
hodbody
).

Acl
asscont
aini
ngabst
ractmet
hodsshoul
dal
sobeabst
ract
.

Wecannotcr
eat
eobj
ect
sofanabst
ractcl
ass.

Toimplementfeat
uresofanabst
ractcl
ass,
wei
nher
itsubcl
assesf
rom i
tandcr
eat
eobj
ect
sof
thesubcl
ass.

Asubclassmustoverr
ideal
labst
ractmet
hodsofanabstr
actclass.Howev
er,
ift
hesubcl
assi
s
decl
aredabst
ract
,it
'snotmandat
orytoover
ri
deabst
ractmethods.

Wecanaccesst hestat
icatt
ri
but
esandmet
hodsofanabst
ractcl
assusi
ngt
her
efer
enceoft
he
abst
ractcl
ass.Forexample,

Ani
mal
.st
ati
cMet
hod(
);

You might also like