PDA

Click to See Complete Forum and Search --> : are variables also initialised in the constructor of the superclass?


vb_student
Oct 26th, 2006, 10:38 AM
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/article.php/10925_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?

vagabon
Oct 26th, 2006, 10:48 AM
If you have a super class with class scope variables they will be inherited(sp?).

public class animal
{
int legs = 4;
...
}

and then

public class dog extends animal
{
...
}

The class dog will have access to the int legs with the value of four.

CornedBee
Oct 26th, 2006, 11:55 AM
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.

ComputerJy
Oct 26th, 2006, 07:12 PM
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

vb_student
Oct 27th, 2006, 07:14 AM
thanks for the replies

so local variables to the superclass will be initialised in the constructor of the superclass, correct?
what is lazy initialisation?

ComputerJy
Oct 27th, 2006, 07:30 AM
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

CornedBee
Oct 27th, 2006, 09:35 AM
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.)

vb_student
Oct 29th, 2006, 07:24 AM
Thanks For The Replies