This print for content only-size & color not accurate 7" X 9-1/4" / casebound / malloy


// Error! Can't access protected data from object instance



Download 21,46 Mb.
Pdf ko'rish
bet400/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   396   397   398   399   400   401   402   403   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// Error! Can't access protected data from object instance.

Employee emp = new Employee();

emp.empName = "Fred";

}

C H A P T E R   6  



U N D E R S TA N D I N G   I N H E R I TA N C E  A N D   P O LY M O R P H I S M



194

8849CH06.qxd  10/1/07  10:35 AM  Page 194






Note

Although 

protected

field data can break encapsulation, it is quite safe (and useful) to define 

protected

methods. When building class hierarchies, it is very common to define a set of methods that are only for use by

derived types.

Adding a Sealed Class

Recall that a sealed class cannot be extended by other classes. As mentioned, this technique is most

often used when you are designing a utility class. However, when building class hierarchies, you

might find that a certain branch in the inheritance chain should be “capped off,” as it makes no

sense to further extend the linage. For example, assume you have added yet another class to your

program (PTSalesPerson) that extends the existing SalesPerson type. Figure 6-5 shows the current

update.

Figure 6-5.

The PTSalesPerson class

PTSalesPerson is a class representing (of course) a part-time salesperson. For the sake of

argument, let’s say that you wish to ensure that no other developer is able to subclass from

PTSalesPerson. (After all, how much more part-time can you get than “part-time”?) Again, to

prevent others from extending a class, make use of the sealed keyword:

sealed class PTSalesPerson : SalesPerson

{

public PTSalesPerson(string fullName, int age, int empID,



float currPay, string ssn, int numbOfSales)

:base (fullName, age, empID, currPay, ssn, numbOfSales)

{

}

// Assume other members here...



}

C H A P T E R   6  



U N D E R S TA N D I N G   I N H E R I TA N C E  A N D   P O LY M O R P H I S M



195

8849CH06.qxd  10/1/07  10:35 AM  Page 195




Given that sealed classes cannot be extended, you may wonder if it is possible to reuse the

code within a class marked sealed. If you wish to build a new class that leverages the functionality

of a sealed class, your only option is to forego classical inheritance and make use of the contain-

ment/delegation model (aka the “has-a” relationship).




Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   396   397   398   399   400   401   402   403   ...   1830




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish