— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

A new 
Hippo 
object also IS-A 
Animal
 
and IS-A 
Object
. If you want to make a
Hippo
, you must also make the 
Animal 
and 
Object
 parts of the 
Hippo

This all happens in a process called 
Constructor Chaining
.
Hippo
A single Hippo object on the heap
Object
a
Y
X
k
V
S
C
b
Animal
Animal
Object
HIppo


252
 
chapter 9
1
Code from another 
class says 
new 
Hippo()
and the 
Hippo() constructor 
goes into a stack 
frame at the top of 
the stack.
Hippo()
Animal()
Hippo()
2
Hippo() invokes 
the superclass 
constructor which 
pushes the Animal() 
constructor onto the 
top of the stack. 
Object()
Hippo()
Animal()
3
4
Object() completes, 
and its stack frame 
is 
popped off the 
stack. Execution goes 
back to the Animal() 
constructor, and 
picks up at the line 
following Animal’s 
call to its superclass 
constructor
Animal() invokes 
the superclass 
constructor which 
pushes the Object() 
constructor onto 
the top of the stack
since Object is the 
superclass of Animal.
Animal()
Hippo()
Making a Hippo means making the 
Animal and Object parts too...
public class Animal {
public Animal() {
System.out.println(“Making an Animal”);
}
}
public class Hippo extends Animal {
public Hippo() {
System.out.println(“Making a Hippo”);
}
}
public class TestHippo {
public static void main (String[] args) {
System.out.println(“Starting...”);
Hippo h = new Hippo();
}
}
File Edit Window Help Swear
% java TestHippo
Starting...
Making an Animal
Making a Hippo
File Edit Window Help Swear
% java TestHippo
Starting...
Making a Hippo
Making an Animal
Sharpen your pencil
A
B
What’s the real output? Given the 
code on the left, what prints out 
when you run TestHippo? A or B?
(the answer is at the bottom of the page)
object 
construction
The first one, A. 
The Hippo() constructor is invoked first, but 
it’s the 
Animal constructor that finishes first.


constructors and 
gc
you are here
4
253
How do you invoke a superclass constructor? 
And how is it that we’ve 
gotten away without 
doing it?
You might think that somewhere in, say, a Duck constructor, 
if Duck extends Animal you’d call Animal(). But that’s not 
how it works:
public class Duck extends Animal {
int size;
public Duck(int newSize) {

Animal();
size = newSize;
}
}
NO! This is not legal!
BAD! 
The only way to call a super constructor is by calling super(). 
That’s right—super() calls the super constructor. 
What are the odds?
public class Duck extends Animal {
int size;
public Duck(int newSize) {

super();
size = newSize;
}
}
you just say super()
A call to super() in your constructor puts the superclass 
constructor on the top of the Stack. And what do you 
think that superclass constructor does? Calls its superclass 
constructor. And so it goes until the Object constructor is 
on the top of the Stack. Once Object() finishes, it’s popped 
off the Stack and the next thing down the Stack (the 
subclass constructor that called Object()) is now on top. 
That constructor finishes and so it goes until the original 
constructor is on the top of the Stack, where it can now 
finish.
You probably figured that out.
Our good friend the compiler 
puts in a call to super() if you 
don’t.
 
So the compiler gets involved in 
constructor-making in two ways:
The compiler puts one in that looks like:
 public ClassName() {
super();
}
The compiler will put a call to super() in 
each of your overloaded constructors.* 
The compiler-supplied call looks like:
super();
It always looks like that. The compiler-
inserted call to super() is always a no-arg 
call. If the superclass has overloaded 
constructors, only the no-arg one is called.
1
2
If you don’t provide a constructor
If you do provide a constructor 
but you do not put in the call to 
super()
*Unless the constructor calls another overloaded 
constructor (you’ll see that in a few pages).



Download 40,57 Mb.

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