— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

Animal myDog = new Dog();
Animal
myDog
Dog object 
These two are NOT the same type. The 
reference variable type is declared as Animal, 
but the object is created as new Dog().


186
 
chapter 7
With polymorphism, the reference 
type can be a superclass of the 
actual object type.
uh... nope. 
Still not gettin’ it.
OK, OK maybe an example will help.
polymorphism in 
action
Animal[] animals = new Animal[5];
animals [0] = new Dog();
animals [1] = new Cat();
animals [2] = new Wolf();
animals [3] = new Hippo();
animals [4] = new Lion();
for (int i = 0; i < animals.length; i++) {

animals[i].eat();

animals[i].roam();
}
Declare an array of type Animal. In other words, 
an array that will hold objects of type Animal.
But look what you get to do... you can put ANY 
subclass of Animal in the Animal array!
And here’s the best polymorphic part (the 
raison d’être for the whole example), you 
get to loop through the array and call one 
of the Animal-class methods, and every 
object does the right thing!
When ‘i’ is 0, a Dog is at index 0 in the array, so 
you get the Dog’s eat() method. When ‘i’ is 1, you 
get the Cat’s eat() method
Same with roam().
When you declare a reference variable, 
any object that passes the IS-A test for the 
declared type of the reference variable 
can be assigned to that reference. In 
other words, anything that extends the 
declared reference variable type can 
be assigned to the reference 
variable. This lets you do 
things like make polymorphic 

Download 40,57 Mb.

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