|
-
Oct 26th, 2006, 09:38 AM
#1
Thread Starter
Frenzied Member
are variables also initialised in the constructor of the superclass?
Hello
I was reading about the construction of constructors and this article says
The first thing that happens inside the constructor is that the constructor of the class's superclass is called.
Then, each class attribute of the object is initialized. These are the attributes that are part of the class definition (instance variables), not the attributes inside the constructor or any other method (local variables). In the DataBaseReader code presented earlier, the integer startPosition is an instance variable of the class.
Then, the rest of the code in the constructor executes.
(http://www.developer.com/design/arti...0925_3516911_3)
if there are any local variables to the superclass, will these variables be initialised in the constructor of the superclass.
w.e. if we have two classes dog and cat each derived from a superclass animal. will variables to do with say number of legs = 4 be initialised within the seuperclass animal?
-
Oct 26th, 2006, 09:48 AM
#2
Lively Member
Re: are variables also initialised in the constructor of the superclass?
If you have a super class with class scope variables they will be inherited(sp?).
Code:
public class animal
{
int legs = 4;
...
}
and then
Code:
public class dog extends animal
{
...
}
The class dog will have access to the int legs with the value of four.
-
Oct 26th, 2006, 10:55 AM
#3
Re: are variables also initialised in the constructor of the superclass?
 Originally Posted by vb_student
w.e. if we have two classes dog and cat each derived from a superclass animal. will variables to do with say number of legs = 4 be initialised within the seuperclass animal?
Yes. A constructor is always only responsible for the content of its immediate class, not any superclasses.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 26th, 2006, 06:12 PM
#4
Re: are variables also initialised in the constructor of the superclass?
each class attribute of the object is initialized.
These are the attributes that are part of the class definition (instance variables), not the attributes inside the constructor or any other method (local variables).
I guess this part is about one example, or maybe the author of the article haven't heard of lazy initialization. Or maybe he means assigning JVM references
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 27th, 2006, 06:14 AM
#5
Thread Starter
Frenzied Member
Re: are variables also initialised in the constructor of the superclass?
thanks for the replies
so local variables to the superclass will be initialised in the constructor of the superclass, correct?
what is lazy initialisation?
-
Oct 27th, 2006, 06:30 AM
#6
Re: are variables also initialised in the constructor of the superclass?
 Originally Posted by vb_student
thanks for the replies
so local variables to the superclass will be initialised in the constructor of the superclass, correct?
what is lazy initialisation?
Lazy initialization is when you initialize local attribute upon usage.
In other words you don't initialize a Object until you want to use it
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 27th, 2006, 08:35 AM
#7
Re: are variables also initialised in the constructor of the superclass?
That's an implementation technique, though, and has nothing to do with the technical side. You still have to initialize the object to the sentinel value in the constructor. (Of course, as that value is usually null, the compiler/JVM does it for you.)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 29th, 2006, 07:24 AM
#8
Thread Starter
Frenzied Member
Re: are variables also initialised in the constructor of the superclass?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|