— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

inheritance and 
polymorphism
you are here
4
183
Inheritance lets you guarantee that 
all classes grouped under a certain 
supertype have all the methods that 
the supertype has.* 
In other words, you define a common protocol for a 
set of classes related through inheritance.
When you defi ne methods in a superclass, that can be 
inherited by subclasses, you’re announcing a kind of 
protocol to other code that says, “All my subtypes (i.e. 
subclasses) can do these things, with these methods 
that look like this...”
In other words, you establish a contract
Class Animal establishes a common protocol for all 
Animal subtypes:
Animal
makeNoise()
eat()
sleep()
roam()
You’re telling the world that 
any Animal can do these four 
things. That includes the method 
arguments and return types.
*When we say “all the methods” we mean “all the inheritable methods”, which 
for now actually means, “all the public methods”, although later we’ll refine that 
definition a bit more.
And remember, when we say any Animal, we mean 
Animal and any class that extends from Animal. Which 
again means, any class that has Animal somewhere above it 
in the inheritance hierarchy.
But we’re not even at the really cool part yet, because 
we saved the best—polymorphism—for last.
When you defi ne a supertype for a group of classes, 
any subclass of that supertype can be substituted where the 
supertype is expected. 
Say, what?
Don’t worry, we’re nowhere near done explaining it. 
Two pages from now, you’ll be an expert.
And I care because...
Because you get to take advantage of 
polymorphism.
Which matters to me 
because...
Because you get to refer to a subclass 
object using a reference declared as the 
supertype.
And that means to me...
You get to write really fl exible code. 
Code that’s cleaner (more effi cient, 
simpler). Code that’s not just easier to 
develop, but also much, much easier to 
extend, in ways you never imagined at 
the time you originally wrote your code.
That means you can take that tropical 
vacation while your co-workers update 
the program, and your co-workers might 
not even need your source code.
You’ll see how it works on the next page. 
We don’t know about you, but 
personally, we fi nd the whole 
tropical vacation thing 
Download 40,57 Mb.

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