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?
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.
Re: are variables also initialised in the constructor of the superclass?
Quote:
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.
Re: are variables also initialised in the constructor of the superclass?
Quote:
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
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?
Re: are variables also initialised in the constructor of the superclass?
Quote:
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
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.)
Re: are variables also initialised in the constructor of the superclass?