— Scott McNealy, Sun Microsystems Chairman, President and ceo


Looking at the Animal class



Download 40,57 Mb.
Pdf ko'rish
bet167/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   163   164   165   166   167   168   169   170   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

Looking at the Animal class, 
we decide that eat() and 
makeNoise() should be overridden 
by the individual subclasses.
3
Decide if a subclass 
needs behaviors (method 
implementations) that are specifi c 
to that particular subclass type.
designing for 
inheritance 


inheritance and 
polymorphism
you are here
4
173
We look at our classes and see 
that Wolf and Dog might have some 
behavior in common, and the same goes 
for Lion, Tiger, and Cat. 
4
Look for more opportunities to use 
abstraction, by fi nding two or more 
subclasses that might need common 
behavior.
The class hierarchy is starting to shape up. We 
have each subclass override the makeNoise() and 
eat() methods, so that there’s no mistaking a Dog 
bark from a Cat meow (quite insulting to both 
parties). And a Hippo won’t eat like a Lion. 
But perhaps there’s more we can do. We have to 
look at the subclasses of Animal, and see if two 
or more can be grouped together in some way
and given code that’s common to only that new 
group. Wolf and Dog have similarities. So do 
Lion, Tiger, and Cat.
Looking for more inheritance 
opportunities
Animal
size
picture
food
prey
Dog
size
picture
food
prey
Wolf
size
picture
food
prey
Cat
size
picture
food
prey
Tiger
size
picture
food
prey
Hippo
size
picture
food
prey
Lion
makeNoise()
eat()
makeNoise()
eat()
makeNoise()
eat()
makeNoise()
eat()
makeNoise()
eat()
makeNoise()
eat()
Hmmm... I wonder if Lion
Tiger, and Cat would have 
something in common.
Wolf and Dog are both canines... 
maybe there’s something that 
BOTH classes could use...
picture
food
hunger
boundaries
location
makeNoise()
eat()
sleep()
roam()



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   163   164   165   166   167   168   169   170   ...   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