My $.02:
public abstract class Stack {
public Stack() {
super();
this.initializeStack(); // make sure the Stack is initialized properly
}
public abstract void initializeStack();
public abstract void push(Object element);
public abstract Object pop();
public abstract boolean isEmpty();
public void wipeOut() {
Object dummy;
while (!isEmpty())
dummy = this.pop();
this.initializeStack(); // reInitialize
}
}
public class ArrayStack extends Stack { // An implementation of Stack using
an array
.... implementation of the abstract methods using an array
}
public class ListStack extends Stack { // An implementation of Stack using
a linked list
...
}
Anyway, I think it is bad practice to initialize variables at declaration.
I have encountered many situations where the reinitialisation takes place,
the decleration intialized values are often forgotten. So instead of
declaring a variable "int d = 1" is something I only do for local variables
for methods or final ones, not for "normal" instance variables.
At 10:17 AM 1/7/99 -0500, Christopher Hinds wrote:
>This may sound like a stupid question but aside from the obvoius technical
>
>reason(s), can someone please give a real-world example where this could
>be used ( ie.. calling a instanance method(s) in the constructor of the
>superclass before
>the superclass has been completely constructed)?
>
>Nathan Meyers wrote:
>
>> Richard Jones wrote:
>> >
>> > Can someone tell me why this program, when run under
>> > Linux, prints d = 0 (ie. the variable d isn't being
>> > initialized as expected)? According to Bruce Eckel's
>> > book, which is the only reference I have available to
>> > me, d should be initialized to 1, so I suspect this
>> > may be a JVM bug ...
>>
>> Have you tried it on JVMs other than Linux?
>>
>> It looks to me like you're invoking method() before DerivedClass has
>> been fully initialized... not entirely surprising, since the super()
>> constructor must occur before any constructor activity in the derived
>> class. (Try, for example, inserting some code before the super() call
>> and you'll be scolded by the compiler. The initialization of d is
>> comparable activity.)
>>
>> Nathan Meyers
>> [EMAIL PROTECTED]
>>
>>
>> > ---------------------------------------------------------
>> > /* SuperClass.java */
>> >
>> > public abstract class SuperClass
>> > {
>> > protected SuperClass ()
>> > {
>> > method ();
>> > }
>> >
>> > protected abstract void method ();
>> > }
>> > ---------------------------------------------------------
>> >
>> > ---------------------------------------------------------
>> > /* DerivedClass.java */
>> >
>> > public class DerivedClass extends SuperClass
>> > {
>> > public int d = 1;
>> >
>> > public DerivedClass ()
>> > {
>> > super ();
>> > }
>> >
>> > protected void method ()
>> > {
>> > System.out.println ("d = " + d);
>> > }
>> >
>> > public static void main (String[] args)
>> > {
>> > DerivedClass derivedClass = new DerivedClass ();
>> > }
>> > }
>> > ---------------------------------------------------------
>> >
>> > $ java -version
>> > java version "1.1.6"
>> > $ java DerivedClass
>> > d = 0
>> >
>> > --
>> > - Richard Jones. Linux contractor London and SE areas. -
>> > - Very boring homepage at: http://www.annexia.demon.co.uk/ -
>> > - You are currently the 1,991,243,100th visitor to this signature. -
>> > - Original message content Copyright (C) 1998 Richard Jones. -
>
>