— Scott McNealy, Sun Microsystems Chairman, President and ceo


arrays. inheritance and



Download 40,57 Mb.
Pdf ko'rish
bet180/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   176   177   178   179   180   181   182   183   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

arrays.


inheritance and 
polymorphism
you are here
4
187
a
But wait! There’s more!
You can have polymorphic 
arguments and return types.
If you can declare a reference variable 
of a supertype, say, Animal, and assign a 
subclass object to it, say, Dog, think of how 
that might work when the reference is an 
argument to a method...
class Vet {
public void giveShot(Animal a) {
// do horrible things to the Animal at 
// the other end of the ‘a’ parameter
a.makeNoise();
}
}
class PetOwner {
public void start() {

Vet v = new Vet();
Dog d = new Dog();
Hippo h = new Hippo();
v.giveShot(d);
v.giveShot(h);
}
}
The Animal parameter can take ANY 
Animal type as the argument. And when 
the Vet is done giving the shot, it tells the 
Animal to makeNoise(), and whatever Animal 
is really out there on the heap, that’s 
whose makeNoise() method will run.
The Vet’s giveShot() method can take any 
Animal you give it. As long as the object 
you pass in as the argument is a subclass of 
Animal, it will work. 
Dog’s makeNoise() runs
Hippo’s makeNoise() runs


188
 
chapter 7
NOW I get it! If I write 
my code using polymorphic arguments
where I declare the method parameter as a 
superclass type, I can pass in any subclass object at 
runtime. Cool. Because that also means I can write my 
code, go on vacation, and someone else can add new 
subclass types to the program and my methods will 
still work... (the only downside is I’m just making life 
easier for that idiot Jim).

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   176   177   178   179   180   181   182   183   ...   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