— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

overriding 
methods


inheritance and 
polymorphism
you are here
4
191
Method overloading is nothing more than having 
two methods with the same name but different 
argument lists. Period. There’s no polymorphism 
involved with overloaded methods! 
Overloading lets you make multiple versions 
of a method, with different argument lists, for 
convenience to the callers. For example, if you 
have a method that takes only an int, the calling 
code has to convert, say, a double into an int 
before calling your method. But if you overloaded 
the method with another version that takes a 
double, then you’ve made things easier for the 
caller. You’ll see more of this when we look into 
constructors in the object lifecycle chapter.
Since an overloading method isn’t trying to 
fulfill the polymorphism contract defined by its 
superclass, overloaded methods have much more 
flexibility. 
Overloading a method
1
The return types can be 
different.
You’re free to change the return types in 
overloaded methods, as long as the argument lists 
are different. 
2
You can’t change ONLY the 
return type.
If only the return type is different, it’s not a 
valid over
load
—the compiler will assume 
you’re trying to over
ride
the method. And even 
that
won’t be legal unless the return type is 
a subtype of the return type declared in the 
superclass. To overload a method, you MUST 
change the argument list, although you 
can
change the return type to anything.
3
You 
can vary the access 
levels in any direction.
You’re free to overload a method with a method 
that’s more restrictive. It doesn’t matter, since the 
new method isn’t obligated to fulfill the contract of 
the overloaded method.
public class Overloads {
String uniqueID;
public 

Download 40,57 Mb.

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