— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet249/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   245   246   247   248   249   250   251   252   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

public Boop() {
}
public Boop(int i) {
size = i;
}
public Boop(int i) {
size = i;
super();
}
These are OK because 
the compiler will put a 
call to super() in as the 
first statement.
BAD!! This won’t compile! 
You can’t explicitly put 
the call to super() below 
anything else.
˛
˛
˛
˛


constructors and 
gc
you are here
4
255
Superclass constructors with arguments
What if the superclass constructor has arguments? Can you pass something in to 
the super() call? Of course. If you couldn’t, you’d never be able to extend a class 
that didn’t have a no-arg constructor. Imagine this scenario: all animals have a 
name. There’s a getName() method in class Animal that returns the value of the 
name instance variable. The instance variable is marked private, but the subclass 
(in this case, Hippo) inherits the getName() method. So here’s the problem: 
Hippo has a getName() method (through inheritance), but does not have the name 
instance variable. Hippo has to depend on the Animal part of himself to keep the 
name instance variable, and return it when someone calls getName() on a Hippo 
object. But... how does the Animal part get the name? The only reference Hippo 
has to the Animal part of himself is through super(), so that’s the place where 
Hippo sends the Hippo’s name up to the Animal part of himself, so that the 
Animal part can store it in the private name instance variable.
Animal
private String name
Animal(String n)
String getName()
Hippo
Hippo(String n)
[other Hippo-spe-
cific methods]

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   245   246   247   248   249   250   251   252   ...   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