0% found this document useful (0 votes)
0 views

1Java Class and-WPS Office

This document provides an overview of classes and objects in Java, explaining their significance in object-oriented programming. It details how to create classes and instantiate objects, using examples like bicycles and lamps to illustrate the concepts. The document also discusses access modifiers and the relationship between class members and instances.

Uploaded by

David
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

1Java Class and-WPS Office

This document provides an overview of classes and objects in Java, explaining their significance in object-oriented programming. It details how to create classes and instantiate objects, using examples like bicycles and lamps to illustrate the concepts. The document also discusses access modifiers and the relationship between class members and instances.

Uploaded by

David
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Jav

aCl
assandObj
ect
s

Inthi
stutor
ial
,youwi
l
llear
naboutt
heconceptofcl
assesandobj
ect
sinJav
awi
tht
hehel
pof
examples.

Javaisanobj
ect
-or
ient
edprogr
ammi ngl
anguage.Thecor
econceptoft
heobj
ect
-or
ient
ed
approachi
stobr
eakcomplexprobl
emsintosmall
erobj
ects.

Anobj
ecti
sanyent
it
ythathasast
ateandbehav
ior
.Forexampl
e,abi
cycl
eisanobj
ect
.Ithas

St
ates:
idl
e,f
ir
stgear
,et
c

Behav
ior
s:br
aki
ng,
accel
erat
ing,
etc.

Bef
orewel
ear
naboutobj
ect
s,l
et'
sfi
rstknowaboutcl
assesi
nJav
a.

Jav
aCl
ass

Acl
assi
sabl
uepr
intf
ort
heobj
ect
.Bef
orewecr
eat
eanobj
ect
,wef
ir
stneedt
odef
inet
hecl
ass.

Wecanthinkofthecl
assasasketch(
prot
oty
pe)ofahouse.I
tcontai
nsallt
hedetai
l
saboutt
he
f
loor
s,doors,
windows,et
c.Basedont
hesedescr
ipt
ionswebui
ldthehouse.Housei
stheobj
ect
.

Si
ncemanyhousescanbemadef
rom t
hesamedescr
ipt
ion,
wecancr
eat
emanyobj
ect
sfr
om
acl
ass.

Cr
eat
eacl
assi
nJav
a

Wecancr
eat
eacl
assi
nJav
ausi
ngt
hecl
asskey
wor
d.Forexampl
e,

cl
assCl
assName{
/
/fi
elds

/
/met
hods

Her
e,f
iel
ds(
var
iabl
es)andmet
hodsr
epr
esentt
hest
ateandbehav
ioroft
heobj
ectr
espect
ivel
y.

f
iel
dsar
eusedt
ost
oredat
a

met
hodsar
eusedt
oper
for
m someoper
ati
ons

Forourbi
cycl
eobj
ect
,wecancr
eat
ethecl
assas

cl
assBi
cycl
e{

/
/st
ateorf
iel
d

pr
ivat
eintgear=5;

/
/behav
iorormet
hod

publ
i
cvoi
dbr
aki
ng(
){

Sy
stem.
out
.pr
int
ln(
"Wor
kingofBr
aki
ng"
);

Int
heaboveexampl
e,wehavecr
eat
edacl
assnamedBi
cycl
e.I
tcont
ainsaf
iel
dnamedgear
andamethodnamedbraki
ng(
).

Here,Bi
cycl
eisapr ot
otype.Now,wecancreat
eanynumberofbicy
clesusi
ngt
hepr
otot
ype.
And,all
thebicy
cleswill
sharethefi
eldsandmethodsoft
hepr
ototy
pe.

Not
e:Wehaveusedkeywordspr
ivat
eandpubl
i
c.Thesear
eknownasaccessmodi
fi
ers.To
l
ear
nmore,v
isi
tJavaaccessmodifi
ers.
Jav
aObj
ect
s

Anobj
ectiscal
l
edaninstanceofacl
ass.Forexampl
e,supposeBicycl
eisacl
assthen
Mount
ainBi
cycl
e,Spor
tsBi
cycl
e,Tour
ingBi
cycl
e,et
ccanbeconsideredasobj
ectsofthecl
ass.

Cr
eat
inganObj
ecti
nJav
a

Her
eishowwecancr
eat
eanobj
ectofacl
ass.

cl
assNameobj
ect=newcl
assName(
);

/
/forBi
cycl
ecl
ass

Bi
cycl
espor
tsBi
cycl
e=newBi
cycl
e()
;

Bi
cycl
etour
ingBi
cycl
e=newBi
cycl
e()
;

Wehav eusedthenewkey wor


dalongwitht
heconstr
uctoroft
hecl asstocreat
eanobj ect
.
Const
ructor
saresimil
artomethodsandhavethesamenameast hecl ass.Forexample,
Bi
cycl
e()istheconst
ruct
oroftheBicy
clecl
ass.Tol
earnmore,vi
sitJavaConstructor
s.

Her
e,spor
tsBi
cycl
eandtour
ingBi
cycl
ear
ethenamesofobj
ect
s.Wecanuset
hem t
oaccess
f
iel
dsandmethodsoft
heclass.

Asyoucansee,
wehavecr
eat
edt
woobj
ect
soft
hecl
ass.Wecancr
eat
emul
ti
pleobj
ect
sofa
si
ngl
eclassi
nJava.

Not
e:Fi
eldsandmet
hodsofacl
assar
eal
socal
l
edmember
soft
hecl
ass.

AccessMember
sofaCl
ass
Wecanuset
henameofobj
ect
sal
ongwi
tht
he.oper
atort
oaccessmember
sofacl
ass.For
exampl
e,

cl
assBi
cycl
e{

/
/fi
eldofcl
ass

i
ntgear=5;

/
/met
hodofcl
ass

v
oidbr
aki
ng(
){

.
..

/
/cr
eat
eobj
ect

Bi
cycl
espor
tsBi
cycl
e=newBi
cycl
e()
;

/
/accessf
iel
dandmet
hod

spor
tsBi
cycl
e.gear
;

spor
tsBi
cycl
e.br
aki
ng(
);

Int
heaboveexampl
e,wehavecreat
edacl
assnamedBi
cycl
e.I
tincl
udesaf
iel
dnamedgear
andamethodnamedbraki
ng(
).Noti
cet
hestat
ement
,

Bi
cycl
espor
tsBi
cycl
e=newBi
cycl
e()
;

Here,wehavecreat
edanobj
ectofBi
cycl
enamedspor
tsBi
cycl
e.Wet
henuset
heobj
ectt
o
accessthef
iel
dandmethodofthecl
ass.
spor
tsBi
cycl
e.gear-accesst
hef
iel
dgear

spor
tsBi
cycl
e.br
aki
ng(
)-accesst
hemet
hodbr
aki
ng(
)

Wehav ementi
onedthewordmet
hodqui
teaf
ewt
imes.Youwi
l
llear
naboutJav
amet
hodsi
n
det
ail
inthenextchapt
er.

Nowt
hatweunder
standwhati
scl
assandobj
ect
.Let
'sseeaf
ull
ywor
kingexampl
e.

Exampl
e:Jav
aCl
assandObj
ect
s

cl
assLamp{

/
/st
orest
hev
aluef
orl
i
ght

/
/tr
uei
fli
ghti
son

/
/fal
sei
fli
ghti
sof
f

bool
eani
sOn;

/
/met
hodt
otur
nont
hel
i
ght

v
oidt
urnOn(
){

i
sOn=t
rue;

Sy
stem.
out
.pr
int
ln(
"Li
ghton?"+i
sOn)
;

/
/met
hodt
otur
nof
fthel
i
ght

v
oidt
urnOf
f(){

i
sOn=f
alse;

Sy
stem.
out
.pr
int
ln(
"Li
ghton?"+i
sOn)
;
}

cl
assMai
n{

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

/
/cr
eat
eobj
ect
sledandhal
ogen

Lampl
ed=newLamp(
);

Lamphal
ogen=newLamp(
);

/
/tur
nont
hel
i
ghtby

/
/cal
l
ingmet
hodt
urnOn(
)

l
ed.
tur
nOn(
);

/
/tur
nof
fthel
i
ghtby

/
/cal
l
ingmet
hodt
urnOf
f()

hal
ogen.
tur
nOf
f()
;

RunCode

Out
put
:

Li
ghton?t
rue

Li
ghton?f
alse

I
ntheaboveprogram,wehavecreatedacl
assnamedLamp.I
tcont
ainsav
ari
abl
e:i
sOnand
t
womet hods:
turnOn(
)andturnOff
().
I
nsidetheMaincl
ass,
wehavecr
eat
edtwoobj
ect
s:l
edandhal
ogenoft
heLampcl
ass.Wet
hen
usedtheobj
ectst
ocallt
hemet
hodsoft
hecl
ass.

l
ed.
tur
nOn(
)-I
tset
sthei
sOnv
ari
abl
etot
rueandpr
int
stheout
put
.

hal
ogen.
tur
nOf
f()-I
tset
sthei
sOnv
ari
abl
etof
alseandpr
int
stheout
put
.

Thevari
ablei
sOndefi
nedinsidet
heclassisal
socall
edani
nst
ancevari
able.I
tisbecausewhen
wecreateanobj
ectofthecl
ass,iti
scall
edaninst
anceoft
hecl
ass.And,eachinst
ancewil
l
havei
tsowncopyofthev ar
iabl
e.

Thati
s,l
edandhal
ogenobj
ect
swi
l
lhav
ethei
rowncopyoft
hei
sOnv
ari
abl
e.

Exampl
e:Cr
eat
eobj
ect
sinsi
det
hesamecl
ass

Not
ethati
ntheprev
iousexampl
e,wehav
ecr
eat
edobj
ect
sinsi
deanot
hercl
assandaccessed
t
hemembersfrom t
hatcl
ass.

Howev
er,
wecanal
socr
eat
eobj
ect
sinsi
det
hesamecl
ass.

cl
assLamp{

/
/st
orest
hev
aluef
orl
i
ght

/
/tr
uei
fli
ghti
son

/
/fal
sei
fli
ghti
sof
f

bool
eani
sOn;

/
/met
hodt
otur
nont
hel
i
ght

v
oidt
urnOn(
){
i
sOn=t
rue;

Sy
stem.
out
.pr
int
ln(
"Li
ghton?"+i
sOn)
;

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

/
/cr
eat
eanobj
ectofLamp

Lampl
ed=newLamp(
);

/
/accessmet
hodusi
ngobj
ect

l
ed.
tur
nOn(
);

RunCode

Out
put

Li
ghton?t
rue

Her
e,wear
ecr
eat
ingt
heobj
ecti
nsi
det
hemai
n()met
hodoft
hesamecl
ass.

You might also like