— Scott McNealy, Sun Microsystems Chairman, President and ceo


Can you make a method final



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

Can you make a method final, 
without making the whole class 
final? 
A: 
If you want to protect a specific 
method from being overridden, mark 
the method with the 
final
modifier. 
Mark the whole class as final if you 
want to guarantee that none of the 
methods in that class will ever be 
overridden.
there are no
Dumb Questions


190
 
chapter 7
When you override a method from a superclass, you’re agreeing to 
fulfi ll the contract. The contract that says, for example, “I take no 
arguments and I return a boolean.” In other words, the arguments 
and return types of your overriding method must look to the outside 
world exactly like the overridden method in the superclass.
The methods are the contract.
If polymorphism is going to work, the Toaster’s version of the 
overridden method from Appliance has to work at runtime. 
Remember, the compiler looks at the reference type to decide 
whether you can call a particular method on that reference. With 
an Appliance reference to a Toaster, the compiler cares only if class 
Appliance has the method you’re invoking on an Appliance reference. 
But at runtime, the JVM looks not at the reference type (Appliance) but 
at the actual Toaster object on the heap. So if the compiler has already 
approved the method call, the only way it can work is if the overriding 
method has the same arguments and return types. Otherwise, 
someone with an Appliance reference will call turnOn() as a no-
arg method, even though there’s a version in Toaster that takes an 
int. Which one is called at runtime? The one in Appliance. In other 
words, the turnOn(int level) method in Toaster is not an override!
Keeping the contract: rules for overriding

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   179   180   181   182   183   184   185   186   ...   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