— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet219/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   215   216   217   218   219   220   221   222   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

226
 
chapter 8
Classes from 
different inheritance trees 
can implement the 
same interface.
Tiger
Animal
Canine
Hippo
Dog
Wolf
Cat
Lion
Feline
Pet
RoboDog
Robot
Agent
Class RoboDog doesn’t 
come from the Animal 
inheritance tree, but it still 
gets to be a Pet! 
When you use a class as a polymorphic type (like an 
array of type Animal or a method that takes a Canine 
argument), the objects you can stick in that type 
must be from the same inheritance tree. But not just 
anywhere in the inheritance tree; the objects must be 
from a class that is a subclass of the polymorphic type. 
An argument of type Canine can accept a Wolf and a 
Dog, but not a Cat or a Hippo. 
But when you use an interface as a polymorphic 
type (like an array of Pets), the objects can be 
from anywhere in the inheritance tree. The only 
requirement is that the objects are from a class that 
implements the interface. Allowing classes in different 
inheritance trees to implement a common interface 
is crucial in the Java API. Do you want an object 
to be able to save its state to a fi le? Implement the 
Serializable interface. Do you need objects to run 
their methods in a separate thread of execution? 
Implement Runnable. You get the idea. You’ll 
learn more about Serializable and Runnable in later 
chapters, but for now, remember that classes from 
any place in the inheritance tree might need to 
implement those interfaces. Nearly 
any class might 
want to be saveable or runnable.
Better still, a class can implement
multiple interfaces!
A Dog object IS-A Canine, and IS-A Animal, and 
IS-A Object, all through inheritance. But a Dog IS-A 
Pet through interface implementation, and the Dog 
might implement other interfaces as well. You could 
say:

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   215   216   217   218   219   220   221   222   ...   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