You are on page 1of 25

Deccansoft Software Services OOPs

Chapter-4


1

"#$%&'

!" #$%&'(') %+,-. /0%112 3+4$5.2 /,67,'$'.2 8'5%71-0%.(,'2 9':$&(.%'5$2 ;,0<6,&7:(16 = 3+4$5.
/&$%.(,' %'> 9'1.%'.(%.(,'
() ?'>$&1.%'>(') 33;1 5,'5$7. .:&,-): %' $@%670$"

*'+,$ -. /-%0$%01
C CvL8vl LW vL8vl LW 2
W WC8kl nC Wl 1P C8kl nC Wl 1P M ML1PCuS L1PCuS 8
W WC8kl nC C8kl nC Wl 1P Wl 1P C C8!LC1 8!LC1 8CL81l LS 8CL81l LS 10
C CCnS18uC1C8S Anu CnS18uC1C8S Anu u uLS18uC1C8S LS18uC1C8S 14
u uLS18uC1C8 LS18uC1C8 16
W WC8kl nC Wl 1P C8kl nC Wl 1P S1A1l C S1A1l C" " M MLM8L8S LM8L8S 17














Deccansoft Software Services OOPs
Chapter-4


2
Cverv| ew Cverv| ew
C|ass:
A class ls a LemplaLe / skeleLon / blueprlnL for creaLlng an ob[ecL.
Cb[ect:
An Cb[ecL ls an enLlLy LhaL has properLles for valldaLlons, meLhods for funcLlonallLy and evenLs for
deplcLlng Lhe change of sLaLe.
Lvery ob[ecL has Lhe daLa and behavlor wlLh whlch Lhey are dlffered. uaLa assoclaLed aL any glven lnsLance
of Llme ls Lhe sLaLe of an ob[ecL.
Component:
A ready Lo use Lhlrd parLy ob[ecL can be called as a ComponenL. lL can be replaced wlLhouL any changes ln
Lhe appllcaLlon. A componenL ls generally used by a programmer as an ob[ecL.
An appllcaLlon can be called as a collecLlon of relaLed ob[ecLs exchanglng messages wlLh each oLher.
Loosely coupled ob[ecLs are beLLer Lhan LlghLly coupled ob[ecLs l.e. Lhe lesser Lhe lnformaLlon glven Lo
oLher ob[ecLs Lhe beLLer lL ls as Lhe ob[ecLs are loosely coupled Lhe dependencles are less and sLronger
securlLy.
Lvery ob[ect or|ented |anguage shou|d have three features:
LncapsulaLlon
lnherlLance
olymorphlsm
Lncapsu|at|on: 8lndlng of daLa and behavlor l.e. funcLlonallLy of an ob[ecL wlLhln a secured and conLrolled
envlronmenL ls encapsulaLlon.
Inher|tance: 1he rocess of acqulrlng Lhe exlsLlng funcLlonallLy of Lhe parenL and wlLh new added feaLures
and funcLlonallLy by a chlld ob[ecL ls called lnherlLance.
1he advanLages of lnherlLance are Genera||zat|on, Lxtens|b|||ty and keusab|||ty.
lor example: A calculaLor ls a generallzed form of maLhemaLlcal operaLlons where as a SclenLlflc calculaLor
ls an LxLended and Speclflc form.
o|ymorph|sm: An ob[ecL ln dlfferenL forms and ln each form lL exhlblLs Lhe same funcLlonallLy buL
lmplemenLed ln dlfferenL ways.
Ior examp|e: A man who knows more Lhan one language can speak any language he knows. Pere Lhe
funcLlonallLy ls speech and person ls Lhe ob[ecL.
A faculLy can Lake a form of !ava laculLy or MSnL1 faculLy and ln boLh Lhe forms he Leaches, buL whaL he
Leaches dlffers. Pere funcLlonallLy ls Lo Leach and faculLy ls Lhe ob[ecL.

Deccansoft Software Services OOPs
Chapter-4


3
Cb[ect Creat|on and Instant|at|on
ln MS.nL1 when an ob[ecL ls creaLed Lhere ls no way Lo geL Lhe address of an ob[ecL. Cnly Lhe reference Lo
Lhe ob[ecL ls glven Lhrough whlch we can access Lhe members of Lhe class for a glven ob[ecL. When an
ob[ecL ls creaLed all Lhe varlables (value/ reference Lypes) are allocaLed Lhe memory ln heap as a slngle
unlL and defaulL values are seL Lo Lhem based on Lhelr daLa Lypes.
Account Lxamp|e:
Steps to create an Account App||cat|on:
1. CreaLe a new pro[ecL (llle ! new ro[ecL). name: AccounL AppllcaLlon, ro[ecL 1ype: C#, 1emplaLe:
Wlndows AppllcaLlon
2. vlew ! SoluLlon Lxplorer, 8lghL Cllck on ro[ecL ! Add ! Class and name lL as AccounL
3. 1o class, add Lhe followlng code:
Creat|ng Account C|ass
uslng SysLem,
uslng SysLem.1exL,
uslng SysLem.Wlndows.lorms,
namespace AccounLAppllcaLlon
[
class AccounL
[
publlc lnL ld,
publlc SLrlng name,
publlc ueclmal 8alance,
publlc AccounL()
[
Message8ox.Show("Cb[ecL CreaLed"),
}
~AccounL()
[
Message8ox.Show("Cb[ecL uesLroyed"),
}
}
}
Code: 4.1 C#

Creat|ng Account C|ass
ubllc Class AccounL
ubllc ld As lnLeger
ubllc name As SLrlng
ubllc 8alance As ueclmal
Shared Sub new()
Message8ox.Show("Cb[ecL CreaLed")
Lnd Sub
roLecLed Cverrldes Sub llnallze()
Message8ox.Show("Cb[ecL uesLroyed")
Lnd Sub
Lnd Class
Code: 4.1 V8
Deccansoft Software Services OOPs
Chapter-4


4
Lxp|anat|on:
" Account a,
a" ls a reference varlable of Lype AccounL.



" a = new Account(),
CreaLes an ob[ecL of Lype AccounL (allocaLlng memory on heap Lo every member of Lhe AccounL class) and
lLs reference ls asslgned Lo 'a'.
Lvery member allocaLed memory ls seL Lo lLs defaulL value based on Lhe daLa Lype.
1he value of Lhe reference varlable ls reference Lo an ob[ecL on heap.
" Account a, 'Dec|arat|on
" a1 = new Account(), 'In|t|a||zat|on
4. ln Lhe AccounL AppllcaLlon, Change Lhe name of Lhe lorm, lorm1" Lo AccountIorm" and deslgn Lhe
followlng Cul
S. lor every conLrol on Lhe form seL 1exL and name properLles.
6. ln ueslgn vlew, double cllck on every buLLon Lo generaLe evenL handlers ln Code vlew of Lhe lorm











23#4 5)6

Note: "a" |s not an ob[ect of type Account.
Deccansoft Software Services OOPs
Chapter-4


5
Lxp|anat|on
a.Id = 1,
1he above sLaLemenL can be read as: ld" menber of an ob[ecL of Lype AccounL referenced by a" ls seL Lo
1.
Note: lf a reference varlable ls noL lnLlallzed l.e referrlng Lo null, Lhen Lrylng Lo access any member uslng lL
wlll Lhrow a runLlme excepLlon l.e. "Nu||keferenceLxcept|on".
Account a1,
a1 = a //Coples Lhe value of a (reference Lo Lhe ob[ecL) lnLo a1 and Lhus boLh a1" and a" refers Lo Lhe
same ob[ecL.
Note: Cne Cb[ecL can have many references buL one reference varlable cannoL refer Lo many ob[ecLs.

7. Add Lhe followlng code Lo Lhe AccounL lorm l.e. Pandle all Lhe buLLons Cllck evenL (uouble cllck on
buLLon ln deslgn vlew)
Account Iorm Code
uslng SysLem,
uslng SysLem.1exL,
uslng SysLem.Wlndows.lorms,
namespace AccounLAppllcaLlon
[
publlc parLlal class AccounLlorm : lorm
[
AccounL a,
publlc lorm1()
[
lnlLlallzeComponenL(),
}
prlvaLe vold bLnCreaLe_Cllck(ob[ecL sender, LvenLArgs e)
[
a = new AccounL(),
}
prlvaLe vold bLnCeL_Cllck(ob[ecL sender, LvenLArgs e)
[
LxLld.1exL = a.ld.1oSLrlng(),
LxLname.1exL = a.name,
LxL8alance.1exL = a.8alance.1oSLrlng(),
}
prlvaLe vold bLnClear_Cllck(ob[ecL sender, LvenLArgs e)
[
LxLld.1exL = "",
LxLname.1exL = "",
LxL8alance.1exL = "",
}
prlvaLe vold bLnSeL_Cllck(ob[ecL sender, LvenLArgs e)
[
a.ld = lnL.arse(LxLld.1exL),
a.name = LxLname.1exL,
a.8alance = declmal.arse(LxL8alance.1exL),
}
prlvaLe vold bLnuesLroy_Cllck(ob[ecL sender, LvenLArgs e)
[
a = null,
Deccansoft Software Services OOPs
Chapter-4


6
}
prlvaLe vold bLnCC_Cllck(ob[ecL sender, LvenLArgs e)
[
CC.CollecL(),
}
prlvaLe vold bLn1emp_Cllck(ob[ecL sender, LvenLArgs e)
[
AccounL a1 = new AccounL(),
a = a1,
}
prlvaLe vold bLnCeLgeneraLlon_Cllck(ob[ecL sender, LvenLArgs e)
[
Message8ox.Show(CC.CeLCeneraLlon(a).1oSLrlng()),
}
}
}
Code: 4.2 C#

Account Iorm Code
ubllc Class AccounLlorm
ulm a As AccounL

rlvaLe Sub bLnCreaLe_Cllck(8yval sender As SysLem.Cb[ecL, 8yval e As SysLem.LvenLArgs)
Pandles bLnCreaLe.Cllck
ulm a As new AccounL
Lnd Sub

rlvaLe Sub bLnCeL_Cllck(8yval sender As SysLem.Cb[ecL, 8yval e As SysLem.LvenLArgs) Pandles
bLnCeL.Cllck
LxLld.1exL = a.ld.1oSLrlng()
LxLname.1exL = a.name
LxL8alance.1exL = a.8alance.1oSLrlng()
Lnd Sub

rlvaLe Sub bLnClear_Cllck(8yval sender As SysLem.Cb[ecL, 8yval e As SysLem.LvenLArgs)
Pandles bLnClear.Cllck
LxLld.1exL = ""
LxLname.1exL = ""
LxL8alance.1exL = ""
Lnd Sub

rlvaLe Sub bLnSeL_Cllck(8yval sender As SysLem.Cb[ecL, 8yval e As SysLem.LvenLArgs) Pandles
bLnSeL.Cllck
a.ld = lnLeger.arse(LxLld.1exL)
a.name = LxLname.1exL
a.8alance = ueclmal.arse(LxL8alance.1exL)
Lnd Sub

rlvaLe Sub bLnuesLroy_Cllck(8yval sender As SysLem.Cb[ecL, 8yval e As SysLem.LvenLArgs)
Pandles bLnuesLroy.Cllck
a = noLhlng
Lnd Sub
Deccansoft Software Services OOPs
Chapter-4


7
rlvaLe Sub bLnCC_Cllck(8yval sender As SysLem.Cb[ecL, 8yval e As SysLem.LvenLArgs) Pandles
bLnCC.Cllck
CC.CollecL()
Lnd Sub

rlvaLe Sub bLn1emp_Cllck(8yval sender As SysLem.Cb[ecL, 8yval e As SysLem.LvenLArgs)
Pandles bLn1emp.Cllck
ulm a1 As new AccounL
a = a1
Lnd Sub

rlvaLe Sub bLnCeLCeneraLlon_Cllck(8yval sender As SysLem.Cb[ecL, 8yval e As
SysLem.LvenLArgs) Pandles bLnCeLCeneraLlon.Cllck
Message8ox.Show(CC.CeLCeneraLlon(a).1oSLrlng())
Lnd Sub
Lnd Class
Code: 4.2 V8



Deccansoft Software Services OOPs
Chapter-4


8
Work| ng w| th Methods Work| ng w| th Methods
1he meLhods of Lhe ob[ecL are used Lo descrlbe Lhe funcLlonallLy / behavlor.

kecommendat|on: A meLhod name should sLarL wlLh uppercase and parameLers should use came|
noLaLlon.
8. ln Lhe AccounL Class add Lhe followlng deposlL and wlLhdraw meLhod.
Add|ng Depos|t and W|thdraw methods
ubllc Sub ueposlL(8yval amounL As ueclmal)
Me.8alance += amounL
Lnd Sub

ubllc Sub WlLhdraw(8yval amounL As ueclmal)
lf Me.8alance - amounL < 300 1hen
1hrow new AppllcaLlonLxcepLlon("lnsufflclenL 8alance")
Llse
Me.8alance -= amounL
Lnd lf
Lnd Sub
Code: 4.3 V8


Note:
An |nstance method of a class ls always lnvoked uslng a reference Lo an ob[ecL.
Whlle wrlLlng a meLhod ln a class do not seL Lo lL any daLa as parameter or return type,
anyLhlng whlch ls already Lhe daLa member ln Lhe same class.
ln a meLhod "th|s"(CS) ls reference Lo Lhe currenL ob[ecL on whlch Lhe meLhod ls lnvoked.
lf parameLer/local varlable and daLa member of a class have same name, Lhe local varlable
Lakes Lhe precedence over daLa member and lf daLa member has Lo accessed ln such a meLhod
Lhen th|s" (CS) can be used.
1he class should be always programmed ln such a way LhaL, lL's noL speclflc Lo any parLlcular
Lype of appllcaLlon so LhaL Lhe same class can reused ln dlfferenL appllcaLlons / envlronmenL.
lor example, don'L use ln class funcLlonallLy llke Msg8ox or Console.WrlLeLlne or 8uLLon or
Label or 1exL8ox eLc..
Whenever any of the bus|ness ru|es of an ob[ect or the |ntegr|ty of the property or a method
|s v|o|ated, |t shou|d respond by throw|ng a runt|me except|on. lor example ln Lhe above
meLhod W|thdraw( ) we are checklng for balance Lo be a mlnlmum of 300, lf vlolaLed an
excepLlon ls ralsed.
Deccansoft Software Services OOPs
Chapter-4


9
Add|ng Depos|t and W|thdraw methods
publlc vold ueposlL(declmal amounL)
[
Lhls.8alance += amounL,
}
publlc vold WlLhdraw(declmal amounL)
[
lf (Lhls.8alance - amounL < 300)
Lhrow new AppllcaLlonLxcepLlon("lnsufflclenL 8alance"),
else
Lhls.8alance -= amounL,
}
Code: 4.3 C#

9. Add ueposlL (bLnueposlL) and WlLhdraw (bLnWlLhdraw) buLLons and AmounL (LxLAmounL) LexLbox Lo
Lhe AccounLlorm. Add Lhe followlng code Lo Lhe evenL handler of Lhe bLnueposlL and bLnWlLhdraw.
nand||ng W|thdraw and Depos|t events
rlvaLe Sub bLnWlLhdraw_Cllck(8yval sender As SysLem.Cb[ecL, 8yval e As SysLem.LvenLArgs)
Pandles bLnWlLhdraw.Cllck
a.WlLhdraw(ueclmal.arse(LxLAmounL.1exL))
Lnd Sub

rlvaLe Sub bLnueposlL_Cllck(8yval sender As SysLem.Cb[ecL, 8yval e As SysLem.LvenLArgs)
Pandles bLnueposlL.Cllck
a.ueposlL(ueclmal.arse(LxLAmounL.1exL))
Lnd Sub
Code: 4.4 V8

nand||ng W|thdraw and Depos|t events
prlvaLe vold bLnueposlL_Cllck(ob[ecL sender, LvenLArgs e)
[
a.ueposlL(declmal.arse(LxLAmounL.1exL)),
}
prlvaLe vold bLnWlLhdraw_Cllck(ob[ecL sender, LvenLArgs e)
[
a.WlLhdraw(declmal.arse(LxLAmounL.1exL)),
}
Code: 4.4 C#


Deccansoft Software Services OOPs
Chapter-4


10
Work| ng w| th Cb[ ect ropert| es Work| ng w| th Cb[ ect ropert| es
A roperLy encapsulaLes a fleld member of Lhe same class, so LhaL Lhe fleld member ls noL freely
accesslble ouLslde Lhe class and Lhe requlred valldaLlon/resLrlcLlon can be puL over lL.
A properLy ls made of Lwo blocks l.e. Get and Set. CeL block ls lnvoked when Lhe properLy ls used on
8PS and SeL block ls lnvoked when Lhe properLy ls used on LPS.
A properLy procedure doesn'L hold any daLa by lLself lnsLead lL depends on some prlvaLe fleld
member of Lhe class.
A SeL block can valldaLe Lhe Va|ue" before lL ls asslgned Lo Lhe fleld member of Lhe class belng
encapsulaLed by Lhe properLy. Slmllarly Lhe fleld member can be valldaLed before lLs value ls
reLurned ln Lhe CeL block.
uslng a properLy Lhe fleld member can be resLrlcLed wlLh elLher keadCn|y" or Wr|teCn|y" access.
A class whlch has all lLs f|e|d members as r|vate and lf requlred resLrlcLed access Lo Lhese members
ls glven uslng ubllc ropert|es ls called "Iu||y Lncapsu|ated C|ass".
Syntax |n C#:
rlvaLe <uaLaLype> _<properLyname>,
publlc <uaLaLype> <roperLyname>
[
[pr|vate] geL
[
reLurn _<properLyname>,
}
[pr|vate] seL
[
_<properLyname> = va|ue,
}
}

10. Add Lhe properLles for name, 8alance and ld wlLh followlng condlLlons:
8alance should be keadCn|y.
name should accepL greaLer or equal Lo and 3 characLers and less Lhan or equal Lo 20 characLers
only
ld should be seL only once.
LdlL Lhe code ln Lhe AccounL Class as below:
Add|ng propert|es
rlvaLe _ld As lnLeger
rlvaLe _name As SLrlng
rlvaLe _8alance As ueclmal
rlvaLe ldAlreadySeL As 8oolean
ubllc roperLy ld As lnLeger
CeL
8eLurn _ld
Lnd CeL
Deccansoft Software Services OOPs
Chapter-4


11
SeL(8yval value As lnLeger)
lf (ldAlreadySeL) 1hen
1hrow new AppllcaLlonLxcepLlon("ld ls already seL")
Lnd lf
_ld = value
ldAlreadySeL = 1rue
Lnd SeL
Lnd roperLy
ubllc roperLy name As SLrlng
CeL
8eLurn _name
Lnd CeL
SeL(8yval value As SLrlng)
lf (value.LengLh > 8) 1hen
1hrow new AppllcaLlonLxcepLlon("name cannoL be greaLer Lhan 8 characLers")
Lnd lf
_name = value
Lnd SeL
Lnd roperLy
ubllc 8eadCnly roperLy 8alance As ueclmal
CeL
8eLurn _8alance
Lnd CeL
Lnd roperLy
ubllc Sub ueposlL(8yval amounL As ueclmal)
Me._8alance += amounL
Lnd Sub
ubllc Sub WlLhdraw(8yval amounL As ueclmal)
lf Me.8alance - amounL < 300 1hen
1hrow new AppllcaLlonLxcepLlon("lnsufflclenL 8alance")
Llse
Me._8alance -= amounL
Lnd lf
Lnd Sub
Code: 4.S V8

Add|ng propert|es
prlvaLe lnL _ld,
prlvaLe SLrlng _name,
prlvaLe ueclmal _8alance,
publlc declmal 8alance
[
geL
[
reLurn _8alance,
}
}
publlc SLrlng name
[
geL
[
reLurn _name,
Deccansoft Software Services OOPs
Chapter-4


12
}
seL
[
lf (value.LengLh > 8)
Lhrow new AppllcaLlonLxcepLlon("name cannoL be > 8 characLers"),
_name = value,
}
}
prlvaLe bool ldAlreadySeL,
publlc lnL ld
[
geL
[
reLurn _ld,
}
seL
[
lf (ldAlreadySeL)
Lhrow new AppllcaLlonLxcepLlon("ld ls already seL"),
_ld = value,
ldAlreadySeL = Lrue,
}
}
publlc vold ueposlL(declmal amounL)
[
Lhls._8alance += amounL,
}
publlc vold WlLhdraw(declmal amounL)
[
lf (Lhls.8alance - amounL < 300)
Lhrow new AppllcaLlonLxcepLlon("lnsufflclenL 8alance"),
else
Lhls._8alance -= amounL,
}
Code: 4.S C#
11. ln btnSet_C||ck of AccounLlorm
replace a.8alance = ueclmal.arse(LxL8alance.LexL)
wlLh a.Depos|t(declmal.arse(txt8a|ance.1ext))

1o ass|gn the va|ues to an ob[ect
rlvaLe Sub bLnSeL_Cllck(8yval sender As SysLem.Cb[ecL, 8yval e As SysLem.LvenLArgs) Pandles
bLnSeL.Cllck
a.ld = lnLeger.arse(LxLld.1exL)
a.name = LxLname.1exL
a.ueposlL(ueclmal.arse(LxL8alance.1exL))
Lnd Sub
Code: 4.6 V8



Deccansoft Software Services OOPs
Chapter-4


13
1o ass|gn the va|ues to an ob[ect
prlvaLe vold bLnSeL_Cllck(ob[ecL sender, LvenLArgs e)
[
a.ld = lnL.arse(LxLld.1exL),
a.name = LxLname.1exL,
a.ueposlL(declmal.arse(LxL8alance.1exL)),
}
Code: 4.6 C#


Deccansoft Software Services OOPs
Chapter-4


14
Constructors and Destructors Constructors and Destructors
A consLrucLor ls a member meLhod of a class whlch ls auLomaLlcally execuLed / called as soon as Lhe ob[ecL
of LhaL class ls creaLed and Lhus lL ls ldeally used for lnlLlallzlng Lhe fleld members of Lhe ob[ecL.
1. A consLrucLor has same name as Lhe class and doesn'L have a reLurn Lype noL even vold.
2. A consLrucLor wlLhouL a parameLer ls called defau|t constructor and Lhe one wlLh parameLers ls
called as arameter|zed constructor.
3. lf a class doesn't have any form of constructor, a pub||c defau|t constructor ls auLomaLlcally added
Lo lL by Lhe language compller.
4. Copy ConsLrucLor ls used Lo creaLe a new ob[ecL by dup||cat|ng Lhe sLaLe of an exlsLlng ob[ecL and Lhls
ls done by copylng Lhe daLa members of exlsLlng ob[ecL ln new ob[ecL daLa members.
Syntax |n C#:
<Class name>()
[
//defau|t constructor
}
<Class name>(<daLaLype> p1, .)
[
//parameter|zed constructor
}
<Class name>(<Class name> <parname>) : th|s(. . .)
[
//copy constructor
}

12. Add Lhe lollowlng code Lo Lhe AccounL Class
Add|ng parameter|zed constructors
ubllc Sub new()
Lnd Sub

ubllc Sub new(8yval ld As lnLeger, 8yval name As SLrlng, 8yval balance As ueclmal)
Me.ld = ld
Me.name = name
Me._8alance = balance
Lnd Sub

ubllc Sub new(8yval a As AccounL)
Me.new(a.ld, a.name, a.8alance)
'Me.ld = ld
'Me.name = name
'Me._8alance = 8alance
Lnd Sub
Code: 4.7 V8



Deccansoft Software Services OOPs
Chapter-4


15
Add|ng parameter|zed constructors
publlc AccounL()
[ }
publlc AccounL(lnL ld, SLrlng name, declmal balance)
: Lhls()
[
Lhls.ld = ld,
Lhls.name = name,
Lhls._8alance = balance,
}
publlc AccounL(AccounL a)
: Lhls(a.ld, a.name, a.8alance)
[
//Lhls.ld = a.ld,
//Lhls.name = a.name,
//Lhls._8alance = a.8alance,
}
Code: 4.7 C#

13. ln AccountIorm make Lhe followlng Changes:
Changes |n c||ck event of Create
rlvaLe Sub bLnCreaLe_Cllck(8yval sender As SysLem.Cb[ecL, 8yval e As SysLem.LvenLArgs) Pandles
bLnCreaLe.Cllck
ulm ld As lnLeger = lnLeger.arse(LxLld.1exL)
ulm name As SLrlng = LxLname.1exL
ulm lnlLlal8alance As ueclmal = ueclmal.arse(LxL8alance.1exL)
ulm a As new AccounL(ld, name, lnlLlal8alance)
Lnd Sub
Code: 4.8 V8

Changes |n c||ck event of Create
prlvaLe vold bLnCreaLe_Cllck(ob[ecL sender, LvenLArgs e)
[
lnL ld = lnL.arse(LxLld.1exL),
sLrlng name = LxLname.1exL,
declmal lnlLlal8alance = declmal.arse(LxL8alance.1exL),
a = new AccounL(ld,name, lnlLlal8alance),
}
Code: 4.8 C#



Note:
Cnce the above code |s changed |n Account Iorm, btnSet_C||ck |s no more usefu| and thus |t can be
removed.
Deccansoft Software Services OOPs
Chapter-4


16
Destructor Destructor
A desLrucLor ls used Lo release Lhe resources LhaL an ob[ecL ls holdlng. When an ob[ecL ls ready Lo be
desLroyed I|na||ze meLhod / uesLrucLor ls called on LhaL ob[ecL.
ln C++, desLrucLor of a class ls responslble for desLroylng all Lhe oLher ob[ecLs whlch Lhe
ob[ecL of Lhls class has creaLed durlng lLs llfeLlme. (ln C++ we don'L have Carbage collecLor)
ln .nL1, all Lhe dependenL ob[ecLs are auLomaLlcally ready for Carbage CollecLlon when Lhe
maln ob[ecL doesn'L have any references Lhus Lhe desLrucLor here doesn'L have same
slgnlflcance as ln C++.



8ecause lL's noL predlcLable when Lhe uesLrucLor meLhod wlll be execuLed, lL ls noL recommended Lo only
rely on desLrucLor for releaslng of resources and Lhus MlcrosofL has suggesLed Lwo meLhods LhaL can be
lnvoked by Lhe programmer for Lhe release of resources l.e. C|ose() or D|spose().

Syntax |n C#:
Syntax |n C#:
~<class name>()
[ }.
Note: A desLrucLor ln C# cannoL have any access modlflers
Deccansoft Software Services OOPs
Chapter-4


17
Work| ng w| th "stat| c" Members Work| ng w| th "stat| c" Members
lor every new lnsLance of a class all Lhe lnsLance members are allocaLed memory, buL sLaLlc fleld
members of a class are allocaLed memory on|y once lrrespecLlve of Lhe number of ob[ecLs creaLed and
Lhey are allocaLed memory when Lhe c|ass |s |oaded. 1hese members are also called c|ass members and
are accessed ouLslde Lhe class uslng class name.

Stat|c Constructor: A consLrucLor ls deflned wlLh Lhe keyword as sLaLlc (ln C#). lL ls used Lo lnlLlallze Lhe
sLaLlc member dynam|ca||y and ls execuLed when Lhe class ls loaded.
1hls ls lnvoked by Lhe CL8 when Lhe class ls loaded by lL and hence cannoL be over|oaded nor can be
declared wlLh any accesses spec|f|er llke publlc or prlvaLe.
Ser|es of events that occur when the f|rst ob[ect |s created:
1. Class ls loaded.
2. SLaLlc members are loaded and allocaLed memory.
3. SLaLlc consLrucLor ls execuLed.
4. lnsLance members are loaded and allocaLed memory.
S. lnsLance consLrucLor ls execuLed.

erform the fo||ow|ng steps sequent|a||y
14. CreaLe a new sLaLlc varlable ln AccounL class:
publlc sLaLlc lnL Mln8alance = 300, //Add Lhls ln AccounL class

1S. ln WlLhdraw meLhod replace Lhe consLanL value 300 wlLh Lhe sLaLlc member "M|n8a|ance":
lf (Lhls.8alance - amounL < Mln8alance) //replace 300 wlLh Mln8alance

16. ln lorm: Add btnGetM8 and btnSetM8 buLLons and txtM8 LexLbox wlLh Lhe followlng code:
prlvaLe vold bLnCeLM8_Cllck(ob[ecL sender, LvenLArgs e)
[
LxLM8.1exL = AccounL.Mln8alance.1oSLrlng(),
Note:
1. A c|ass |s |oaded when e|ther the stat|c members of the c|ass |s accessed for the f|rst t|me
or when the f|rst |nstance of the c|ass |s created. A c|ass once |oaded w||| rema|n |n
memory permanent|y and thus a|so are a|| stat|c members of that c|ass.
2. A pub||c stat|c member of a c|ass can be used as G|oba| member of the app||cat|on
because |t can be assessed us|ng c|ass name from any part of the app||cat|on.
Note:
Stat|c Constructor cannot access Instance members of the c|ass. 1h|s |s because when the stat|c
constructor |s executed the |nstance members are not a||ocated any memory.
Deccansoft Software Services OOPs
Chapter-4


18
}

prlvaLe vold bLnSeLM8_Cllck(ob[ecL sender, LvenLArgs e)
[
AccounL.Mln8alance = lnL.arse(LxLM8.1exL),
}
17. CreaLe a new stat|c varlable ln AccounL class:
publlc sLaLlc lnL _revld,
18. Make Lhe followlng changes ln defaulL consLrucLor of AccounL class Lo auLogeneraLe Lhe nexL lu
lnsLead of user enLerlng a value:
publlc AccounL()
[
_revld += 1,
_ld = _revld,
}
19. ld now should be changed Lo a 8eadCnly properLy as we are auLogeneraLlng Lhe new lus. lor
Lhls we can remove Lhe seLLer as shown below:

publlc lnL ld
[
geL
[
reLurn _ld,
}
//seL
//[
// lf (ldAlreadySeL)
// Lhrow new AppllcaLlonLxcepLlon("ld ls already seL"),
// _ld = value,
// ldAlreadySeL = Lrue,
//}
}
20. 8ecause ld" ls now auLo lncremenL fleld generaLe by defaulL consLrucLor, remove Lhe ld
parameLer from parameLerlzed consLrucLor of AccounL Class:
ubllc Sub new(8yval name As SLrlng, 8yval balance As ueclmal)
Me.name = name
Me._8alance = balance
Lnd Sub

ubllc Sub new(8yval a As AccounL)
Me.new(a.name, a.8alance)
Lnd Sub
20. 8emove ld" argumenL whlle creaLlng Lhe AccounL ob[ecL ln bLnCreaLe_Cllck of AccounLlorm:
rlvaLe Sub bLnCreaLe_Cllck(8yval sender As SysLem.Cb[ecL, 8yval e As SysLem.LvenLArgs) Pandles
bLnCreaLe.Cllck
'ulm ld As lnLeger = lnLeger.arse(LxLld.1exL)
ulm name As SLrlng = LxLname.1exL
ulm lnlLlal8alance As ueclmal = ueclmal.arse(LxL8alance.1exL)
ulm a As new AccounL(name, lnlLlal8alance) '8emoved ld argumenLs
Lnd Sub

21. 8emove a.ld" argumenL for updaLlng Lhe AccounL ob[ecL ln bLnSeL_Cllck
of AccounLlorm:
Deccansoft Software Services OOPs
Chapter-4


19
prlvaLe vold bLnSeL_Cllck(ob[ecL sender, LvenLArgs e)
[
//a.ld = lnL.arse(LxLld.1exL),
a.name = LxLname.1exL,
a.ueposlL(declmal.arse(LxL8alance.1exL)),
}
Code: 4.9 V8

erform the fo||ow|ng steps sequent|a||y
14. CreaLe a new Shared varlable ln AccounL class:
ubllc Shared Mln8alance As lnLeger = 300 'Add Lhls ln AccounL class
1S. ln WlLhdraw meLhod replace Lhe consLanL value 300 wlLh Lhe sLaLlc member "M|n8a|ance":
lf Me.8alance - amounL < Mln8alance 1hen 'replace 300 wlLh Mln8alance
16. ln lorm: Add btnGetM8 and btnSetM8 buLLons and txtM8 LexLbox wlLh Lhe followlng code:
rlvaLe Sub bLnCeLM8_Cllck(8yval sender As SysLem.Cb[ecL, 8yval e As SysLem.LvenLArgs)
Pandles bLnCeLM8.Cllck
LxLM8.1exL = AccounL.Mln8alance.1oSLrlng()
Lnd Sub
rlvaLe Sub bLnSeLM8_Cllck(8yval sender As SysLem.Cb[ecL, 8yval e As SysLem.LvenLArgs) Pandles
bLnSeLM8.Cllck
AccounL.Mln8alance = lnLeger.arse(LxLM8.1exL)
Lnd Sub
17. CreaLe a new stat|c varlable ln AccounL class:
ubllc Shared _revld As lnLeger
18. Make Lhe followlng changes ln defaulL consLrucLor of AccounL class Lo auLogeneraLe Lhe nexL lu
lnsLead of user enLerlng a value:
ubllc Sub new()
_revld += 1
_ld = _revld
Lnd Sub
19. ld now should be changed Lo a 8eadCnly properLy as we are auLogeneraLlng Lhe new lus. lor
Lhls we can remove Lhe seLLer as shown below:
ubllc 8eadCnly roperLy ld As lnLeger 'Add 8eadCnly keyword Lo roperLy deflnlLlon
CeL
8eLurn _ld
Lnd CeL
'SeL(8yval value As lnLeger)
' lf (ldAlreadySeL) 1hen
' 1hrow new AppllcaLlonLxcepLlon("ld ls already seL")
' Lnd lf
' _ld = value
' ldAlreadySeL = 1rue
'Lnd SeL
Lnd roperLy
20. 8ecause ld" ls now auLo lncremenL fleld generaLe by defaulL consLrucLor, remove Lhe ld
parameLer from parameLerlzed consLrucLor of AccounL Class:
ubllc Sub new(8yval name As SLrlng, 8yval balance As ueclmal)
Me.name = name
Me._8alance = balance
Lnd Sub
ubllc Sub new(8yval a As AccounL)
Me.new(a.name, a.8alance)
Deccansoft Software Services OOPs
Chapter-4


20
Lnd Sub
20. 8emove ld" argumenL whlle creaLlng Lhe AccounL ob[ecL ln bLnCreaLe_Cllck of AccounLlorm:
rlvaLe Sub bLnCreaLe_Cllck(8yval sender As SysLem.Cb[ecL, 8yval e As SysLem.LvenLArgs) Pandles
bLnCreaLe.Cllck
'ulm ld As lnLeger = lnLeger.arse(LxLld.1exL)
ulm name As SLrlng = LxLname.1exL
ulm lnlLlal8alance As ueclmal = ueclmal.arse(LxL8alance.1exL)
ulm a As new AccounL(name, lnlLlal8alance) '8emoved ld argumenLs
Lnd Sub
21. 8emove a.ld" argumenL for updaLlng Lhe AccounL ob[ecL ln bLnSeL_Cllck
of AccounLlorm:
rlvaLe Sub bLnSeL_Cllck(8yval sender As SysLem.Cb[ecL, 8yval e As SysLem.LvenLArgs) Pandles
bLnSeL.Cllck
'a.ld = lnLeger.arse(LxLld.1exL)
a.name = LxLname.1exL
a.ueposlL(ueclmal.arse(LxL8alance.1exL))
Lnd Sub
Code: 4.9 C#

Screenshots of sequent|a| execut|on of the program:
1. 8un Lhe program


2. LnLer name and balance and cllck on CreaLe

Deccansoft Software Services OOPs
Chapter-4


21

3. Cllck on Clear

4. Cllck on CeL


S. Change Lhe name and cllck on SeL


6. Cllck on Clear agaln
Deccansoft Software Services OOPs
Chapter-4


22


7. Cllck on CeL Lo check wheLher Lhe changes were saved.


8. LnLer ueposlL amounL and cllck on ueposlL.


9. Cllck on CeL Lo see Lhe updaLed balance.
Deccansoft Software Services OOPs
Chapter-4


23


10. LnLer M8(Mlnlmum 8alance) (1000 as shown below) and cllck on SeL M8 buLLon.


11. 1ry Lo wlLhdraw amounL LhaL wlll leave Lhe accounL wlLh less Lhan mlnlmum balance.


12. Lrror ls dlsplayed. ?ou can cllck on ConLlnue buL Lhe recenL wlLhdrawal wlll noL be saved.
Deccansoft Software Services OOPs
Chapter-4


24


Stat|c Methods:
A meLhod whlch does noL have anyLhlng Lo do wlLh Lhe sLaLe of Lhe ob[ecL can be marked as sLaLlc
meLhod.
Lg: class llle
[
publlc stat|c vold Copy(sLrlng srcaLh,sLrlng desLaLh)
[ . . . }
}
1. CuLslde Lhe class a sLaLlc members of a class can be accessed uslng class name where as Lhe lnsLance
member of a class musL be accessed uslng a reference varlable referrlng Lo an ob[ecL of LhaL class.
2. lnsLance members of Lhe class cannoL be accessed ln a sLaLlc consLrucLor or any oLher sLaLlc meLhod or
properLy of Lhe class (unless lL ls quallfled by a reference varlable referrlng Lo an ob[ecL of LhaL class).
lor Lxample
publlc sLaLlc vold loo()
[
//_8alance = 100, // ls lnvalld
AccouL a = new AccounL(),
a._8alance = 100, // lsvalld because _8alance ls quallfled by a" whlch ls reference Lo an
ob[ecL.
}
3. An lnsLance member can access sLaLlc members of Lhe class.
4. Lhls" cannoL be used ln Lhe sLaLlc member of a class.
S. A class ln C# can be declared as sLaLlc and such a class can have only sLaLlc members and lnsLance
members are noL allowed. Also such a class cannoL be lnsLanLlaLed.
stat|c class Demo
[
Note:
A|| methods and propert|es (procedures) of the c|ass |rrespect|ve of whether they are stat|c or not
are a||ocated memory on|y once.
Deccansoft Software Services OOPs
Chapter-4


25
publlc stat|c lnL 1,
publlc stat|c vold loo()
[ }
}

Summary:
ln Lhls secLlon, we have seen Lhe LeneLs of ob[ecL orlenLed programmlng and how one can use Lhese ln
one of Lhe real llfe requlremenLs.

You might also like