— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

174
 
chapter 7
Finish the class hierarchy
Animal
Feline
roam()
Canine
size
picture
food
prey
Lion
size
picture
food
prey
Tiger
size
picture
food
prey
Cat
size
picture
food
prey
Wolf
size
picture
food
prey
Dog
Since animals already have an organizational 
hierarchy (the whole kingdom, genus, phylum 
thing), we can use the level that makes the most 
sense for class design. We’ll use the biological 
“families” to organize the animals by making a 
Feline class and a Canine class.
We decide that Canines could use a common 
roam() method, because they tend to move in 
packs. We also see that Felines could use a 
common roam() method, because they tend to 
avoid others of their own kind. We’ll let Hippo 
continue to use its inherited roam() method—
the generic one it gets from Animal. 
So we’re done with the design for now; we’ll 
come back to it later in the chapter.
5
size
picture
food
prey
Hippo
makeNoise()
eat()
roam()
makeNoise()
eat()
makeNoise()
eat()
makeNoise()
eat()
makeNoise()
eat()
makeNoise()
eat()
picture
food
hunger
boundaries
location
makeNoise()
eat()
sleep()
roam()
designing for 
inheritance 


inheritance and 
polymorphism
you are here
4
175
Wolf w = new Wolf();
w.makeNoise();
w.roam();
w.eat();
w.sleep();
Which method is called?
The Wolf class has four methods. One 
inherited from Animal, one inherited from 
Canine (which is actually an overridden 
version of a method in class Animal), and 
two overridden in the Wolf class. When 
you create a Wolf object and assign it to 
a variable, you can use the dot operator 
on that reference variable to invoke all 
four methods. But which version of those 
methods gets called?
make a new Wolf object
calls the version in Wolf
calls the version in Canine
calls the version in Wolf
calls the version in Animal
size
picture
food
prey

Download 40,57 Mb.

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