Java OOPs Conce
Java OOPs Conce
aOOPsConcept
s
OOPs(
Obj
ect
-Or
ient
edPr
ogr
ammi
ngSy
stem)
Obj
ectmeansar
eal
-wor
ldent
it
ysuchasapen,
chai
r,t
abl
e,comput
er,
wat
ch,
etc.Obj
ect
-
Or
ient
edPr
ogr
ammi
ngi
samet
hodol
ogyorpar
adi
gm t
odesi
gnapr
ogr
am usi
ngcl
assesand
obj
ect
s.I
tsi
mpl
i
fiest
hesof
twar
edev
elopmentandmai
ntenancebypr
ovi
dingsomeconcept
s:
Obj
ect
Cl
ass
I
nher
it
ance
Pol
ymor
phi
sm
Abst
ract
ion
Encapsul
ati
on
Obj
ect
Anyent
it
ythathasst
ateandbehav
iori
sknownasanobj
ect
.Forexampl
eachai
r,pen,
t
abl
e,key
boar
d,bi
ke,
etc.I
tcanbephy
sical
orl
ogi
cal
.
AnObj
ectcanbedef
inedasani
nst
anceofacl
ass.Anobj
ectcont
ainsanaddr
essand
t
akesupsomespacei
nmemor
y.Obj
ect
scancommuni
cat
ewi
thoutknowi
ngt
hedet
ail
s
ofeachot
her
'sdat
aorcode.Theonl
ynecessar
ythi
ngi
sthet
ypeofmessageaccept
ed
andt
het
ypeofr
esponser
etur
nedbyt
heobj
ect
s.
Exampl
e:Adogi
sanobj
ectbecausei
thasst
atesl
i
kecol
or,
name,
breed,
etc.aswel
las
behav
ior
sli
kewaggi
ngt
het
ail
,bar
king,
eat
ing,
etc.
Cl
ass
Col
l
ect
ionofobj
ect
siscal
l
edcl
ass.I
tisal
ogi
cal
ent
it
y.
Acl
asscanal
sobedef
inedasabl
uepr
intf
rom whi
chy
oucancr
eat
eani
ndi
vi
dual
obj
ect
.Cl
assdoesn'
tconsumeanyspace.
Obj
ectandCl
assExampl
e:I
nit
ial
i
zat
iont
hroughr
efer
ence
cl
assSt
udent
i
nti
d;
St
ri
ngname;
cl
assTest
Student
2
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{
St
udents1=newSt
udent
();
s1.
i
d=101;
s1.
name="
Sonoo"
;
Sy
stem.
out
.pr
int
ln(
s1.
id+""
+s1.
name)
;/
/pr
int
ingmember
swi
thawhi
tespace
cl
assSt
udent
{
i
nti
d;
St
ri
ngname;
cl
assTest
Student
3
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
/
/Cr
eat
ingobj
ect
s
St
udents1=newSt
udent
();
St
udents2=newSt
udent
();
/
/Ini
ti
ali
zi
ngobj
ect
s
s1.
i
d=101;
s1.
name="
Sonoo"
;
s2.
i
d=102;
s2.
name="
Ami
t"
;
/
/Pr
int
ingdat
a
Sy
stem.
out
.pr
int
ln(
s1.
id+""
+s1.
name)
;
Sy
stem.
out
.pr
int
ln(
s2.
id+""
+s2.
name)
;
}
Obj
ectandCl
assExampl
e:I
nit
ial
i
zat
iont
hroughmet
hod
cl
assSt
udent
i
ntr
oll
no;
St
ri
ngname;
v
oidi
nser
tRecor
d(i
ntr
,St
ri
ngn)
r
oll
no=r
;
name=n;
v
oiddi
spl
ayI
nfor
mat
ion(
)
Sy
stem.
out
.pr
int
ln(
rol
l
no+""
+name)
;}
cl
assTest
Student
4
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{
St
udents1=newSt
udent
();
St
udents2=newSt
udent
();
s1.
i
nser
tRecor
d(111,
"Kar
an"
);
s2.
i
nser
tRecor
d(222,
"Ar
yan"
);
s1.
displ
ayI
nfor
mat
ion(
);
s2.
displ
ayI
nfor
mat
ion(
);
Obj
ectandCl
assExampl
e:Rect
angl
e
cl
assRect
angl
e
i
ntl
engt
h;
i
ntwi
dth;
v
oidi
nser
t(i
ntl
,i
ntw)
l
engt
h=l
;
wi
dth=w;
v
oidcal
cul
ateAr
ea(
)
{
Sy
stem.
out
.pr
int
ln(
lengt
h*wi
dth)
;
cl
assTest
Rect
angl
e1
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
Rect
angl
er1=newRect
angl
e()
;
Rect
angl
er2=newRect
angl
e()
;
r
1.i
nser
t(11,
5);
r
2.i
nser
t(3,
15)
;
r
1.cal
cul
ateAr
ea(
);
r
2.cal
cul
ateAr
ea(
);
Anony
mousobj
ect
Anony
moussi
mpl
ymeansnamel
ess.Anobj
ectwhi
chhasnor
efer
encei
sknownasan
mousobj
anony ect
.Itcanbeusedatt
het
imeofobj
ectcr
eat
iononl
y.
I
fyouhav
etouseanobj
ectonl
yonce,
ananony
mousobj
ecti
sagoodappr
oach.For
exampl
e:
newCal
cul
ati
on(
);
//anony
mousobj
ect
Cal
l
ingmet
hodt
hroughar
efer
ence:
Cal
cul
ati
onc=newCal
cul
ati
on(
);
c.
fact
(5)
;
Cal
l
ingmet
hodt
hroughananony
mousobj
ect
newCal
cul
ati
on(
).
fact
(5)
;
Let
'sseet
hef
ull
exampl
eofananony
mousobj
ecti
nJav
a.
cl
assCal
cul
ati
on
v
oidf
act
(i
ntn)
i
ntf
act
=1;
f
or(
inti
=1;
i
<=n;
i
++)
f
act
=fact
*i;
Sy
stem.
out
.pr
int
ln(
"fact
ori
ali
s"+f
act
);
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{
newCal
cul
ati
on(
).
fact
(5)
;/
/cal
l
ingmet
hodwi
thanony
mousobj
ect
Out
put
:
Fact
ori
ali
s120
Cr
eat
ingmul
ti
pleobj
ect
sbyonet
ypeonl
y
Jav
aPr
ogr
am t
oil
l
ust
rat
etheuseofRect
angl
ecl
asswhi
ch
/
/hasl
engt
handwi
dthdat
amember
s
cl
assRect
angl
e
i
ntl
engt
h;
i
ntwi
dth;
v
oidi
nser
t(i
ntl
,
intw)
l
engt
h=l
;
wi
dth=w;
v
oidcal
cul
ateAr
ea(
)
Sy
stem.
out
.pr
int
ln(
lengt
h*wi
dth)
;
}
}
cl
assTest
Rect
angl
e2
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
Rect
angl
er1=newRect
angl
e()
,r
2=newRect
angl
e()
;/
/cr
eat
ingt
woobj
ect
s
r
1.i
nser
t(11,
5);
r
2.i
nser
t(3,
15)
;
r
1.cal
cul
ateAr
ea(
);
r
2.cal
cul
ateAr
ea(
);
Jav
aPr
ogr
am t
odemonst
rat
ethewor
kingofabanki
ng-
syst
em
/
/wher
ewedeposi
tandwi
thdr
awamountf
rom ouraccount
.
/
/Cr
eat
inganAccountcl
asswhi
chhasdeposi
t(
)andwi
thdr
aw(
)met
hods
cl
assAccount
i
ntacc_
no;
St
ri
ngname;
f
loatamount
;
/
/Met
hodt
oini
ti
ali
zeobj
ect
v
oidi
nser
t(i
nta,
Str
ingn,
fl
oatamt
)
acc_
no=a;
name=n;
amount
=amt
;
/
/deposi
tmet
hod
v
oiddeposi
t(
fl
oatamt
)
amount
=amount
+amt
;
Sy
stem.
out
.pr
int
ln(
amt
+"deposi
ted"
);
/
/wi
thdr
awmet
hod
v
oidwi
thdr
aw(
fl
oatamt
)
i
f(
amount
<amt
)
Sy
stem.
out
.pr
int
ln(
"I
nsuf
fi
cientBal
ance"
);
el
se
{
amount
=amount
-amt
;
Sy
stem.
out
.pr
int
ln(
amt
+"wi
thdr
awn"
);
/
/met
hodt
ocheckt
hebal
anceoft
heaccount
v
oidcheckBal
ance(
)
Sy
stem.
out
.pr
int
ln(
"Bal
ancei
s:"
+amount
);
/
/met
hodt
odi
spl
ayt
hev
aluesofanobj
ect
v
oiddi
spl
ay(
)
Sy
stem.
out
.pr
int
ln(
acc_
no+""
+name+""
+amount
);
}
/
/Cr
eat
ingat
estcl
asst
odeposi
tandwi
thdr
awamount
cl
assTest
Account
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs)
{
Accounta1=newAccount
();
a1.
i
nser
t(832345,
"Anki
t"
,1000)
;
a1.
displ
ay(
);
a1.
checkBal
ance(
);
a1.
deposi
t(
40000)
;
a1.
checkBal
ance(
);
a1.
wit
hdr
aw(
15000)
;
a1.
checkBal
ance(
);
Testi
tNow
Out
put
:
832345Anki
t1000.
0
Bal
ancei
s:1000.
0
40000.
0deposi
ted
Bal
ancei
s:41000.
0
15000.
0wi
thdr
awn
Bal
ancei
s:26000.
0