— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

constructors and 
gc
you are here
4
251
The role of superclass constructors 
in an object’s life.
All the constructors in an object’s inheritance 
tree must run when you make a new object.
Let that sink in.
That means every superclass has a constructor 
(because every class has a constructor), and each 
constructor up the hierarchy runs at the time an 
object of a subclass is created. 
Saying 
new 
is a Big Deal. It starts the 
whole constructor chain reaction. And yes, 
even abstract classes have constructors. 
Although you can never say new on an 
abstract class, an abstract class is still 
a superclass, so its constructor runs 
when someone makes an instance of a 
concrete subclass.
The super constructors run to build 
out the superclass parts of the object. 
Remember, a subclass might inherit 
methods that depend on superclass state 
(in other words, the value of instance variables 
in the superclass). For an object to be fully-
formed, all the superclass parts of itself must be 
fully-formed, and that’s why the super constructor 
must run. All instance variables from every class 
in the inheritance tree have to be declared and 
initialized. Even if Animal has instance variables 
that Hippo doesn’t inherit (if the variables are 
private, for example), the Hippo still depends on 
the Animal methods that use those variables.
When a constructor runs, it immediately calls its 
superclass constructor, all the way up the chain 
until you get to the class Object constructor. 
On the next few pages, you’ll learn how superclass 
constructors are called, and how you can call 
them yourself. You’ll also learn what to do if your 
superclass constructor has arguments!

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   242   243   244   245   246   247   248   249   ...   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