— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

Toaster_boolean_turnOn(int_level)_This_is_NOT_an_override!_Can’t_change_the_arguments_in_an_overriding_method!_Appliance'>Toaster
boolean turnOn(int level)
This is NOT an 
override!
Can’t change the 
arguments in an 
overriding method!
Appliance
boolean turnOn()
boolean turnOff()
1
Arguments must be the same, and return 
types must be compatible.
The contract of superclass defi nes how other code can use a method. 
Whatever the superclass takes as an argument, the subclass over-
riding the method must use that same argument. And whatever the 
superclass declares as a return type, the overriding method must de-
clare either the same type, or a subclass type. Remember, a subclass 
object is guaranteed to be able to do anything its superclass declares, 
so it’s safe to return a subclass where the superclass is expected.
2
The method can’t be less accessible.
That means the access level must be the same, or friendlier. That 
means you can’t, for example, override a public method and make 
it private. What a shock that would be to the code invoking what it 
thinks (at compile time) is a public method, if suddenly at runtime 
the JVM slammed the door shut because the overriding version 
called at runtime is private!
So far we’ve learned about two access levels: private and public. 
The other two are in the deployment chapter (Release your Code) 
and appendix B. There’s also another rule about overriding related 
to exception handling, but we’ll wait until the chapter on exceptions 
(Risky Behavior) to cover that.
This is actually a legal 
overLOAD, but not an 
overRIDE.
Toaster
private boolean turnOn()
Appliance
public boolean turnOn()
public boolean turnOn()
NOT LEGAL!
It’s not a legal 
override because you 
restricted the access 
level. Nor is it a legal 
overLOAD, because 
you didn’t change 
arguments.

Download 40,57 Mb.

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