— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet206/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   202   203   204   205   206   207   208   209   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

When a Dog loses its 
Dogness
Dog sameDog = getObject(aDog);
Dog sameDog = getObject(aDog);
Dog sameDog = getObject(aDog);
Dog sameDog = getObject(aDog);
We’re returning a reference to the same Dog, but as a 
return type of Object. This part is perfectly legal. Note: 
this is similar to how the get() method works when you have 
an ArrayList rather than an ArrayList.


interfaces and 
polymorphism
you are here
4
213
So now we know that when an object is 
referenced by a variable declared as type 
Object, it can’t be assigned to a variable 
declared with the actual object’s type. 
And we know that this can happen when 
a return type or argument is declared 
as type Object, as would be the case, 
for example, when the object is put 
into an ArrayList of type Object using 
ArrayList. But what are the 
implications of this? Is it a problem to
have to use an Object reference variable 
to refer to a Dog object? Let’s try to call 
Dog methods on our Dog-That-Compiler-
Thinks-Is-An-Object:
Objects don’t bark.
Object o = al.get(index);
int i = o.hashCode();
o.bark(); 
This is fine. Class Object has a 
hashCode() method, so you can call 
that method on ANY object in Java.
Can’t do this!! The Object class has no idea what 
it means to bark(). Even though YOU know it’s 
really a Dog at that index, the compiler doesn’t..
Object
o
D
og
o
b
je
ct
When you get an object reference from 
an ArrayList (or any method 
that declares Object as the return type), 
it comes back as a polymorphic reference 
type of Object. So you have an Object 
reference to (in this case) a Dog instance.
Won’t compile!
The compiler decides whether 
you can call a method based 
on the 
reference type, not the 
actual 
object type.
Even if you know the object is capable 
(“...but it really is a Dog, honest...”), the 
compiler sees it only as a generic Object. 
For all the compiler knows, you put a 
Button object out there. Or a Microwave 
object. Or some other thing that really 
doesn’t know how to bark.
The compiler checks the class of the 
reference type—not the object type—to 
see if you can call a method using that 
reference.
Object
o
D
og
o
b
je
ct
Object
equals()
getClass()
hashCode()
toString()
The method you’re calling on a 
reference MUST be in the class of 
that reference type. Doesn’t matter 
what the actual object is.

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   202   203   204   205   206   207   208   209   ...   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