— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet172/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   168   169   170   171   172   173   174   175   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

inheritance and 
polymorphism
you are here
4
179
Hint: apply the IS-A test
Put a check next to the relationships that
make sense.
Oven extends Kitchen
Guitar extends Instrument
Person extends Employee
Ferrari extends Engine
FriedEgg extends Food
Beagle extends Pet
Container extends Jar
Metal extends Titanium
GratefulDead extends Band
Blonde extends Smart
Beverage extends Martini
Sharpen your pencil
How do you know if you’ve got 
your inheritance right?
There’s obviously more to it than what we’ve 
covered so far, but we’ll look at a lot more OO 
issues in the next chapter (where we eventually 
refi ne and improve on some of the design work 
we did in this chapter).
For now, though, a good guideline is to use the 
IS-A test. If “X IS-A Y” makes sense, both classes 
(X and Y) should probably live in the same 
inheritance hierarchy. Chances are, they have 
the same or overlapping behaviors.
Keep in mind that the 
inheritance IS-A relationship 
works in only 
one direction!
Triangle IS-A Shape makes sense, so you can 
have Triangle extend Shape. 
But the reverse—Shape IS-A Triangle—does 
not make sense, so Shape should not extend 
Triangle. Remember that the IS-A relationship 
implies that if X IS-A Y, then X can do anything 
a Y can do (and possibly more). 
Make it Stick
Roses are
 red, viole
ts are blu
e.
Square is
-a Shape
, the reve
rse isn’t t
rue.
Roses are
 red, viole
ts are dea
r.
Beer is-a
 Drink, b
ut not all
 drinks a
re beer.
OK, your
turn. Ma
ke one th
at shows
the one-
way-ness
of the IS
-A relatio
nship. Re
member
, if 
extend

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   168   169   170   171   172   173   174   175   ...   555




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