Hi,

A quick comment on this technique.  You must be careful when setting variables
in the childClass if you call the init method from the constructor.  For example
the following code produces unexpected results:

class Child extends Parent {
    int value1 = 0;
    int value2;

    public void childInit() {
        value1 = 5;
        value2 = 10;
    }

    public void printValues() {
        System.out.println("value1: " + value1);
        System.out.println("value2: " + value2);
    }
}

public abstract class Parent {
    public Parent() {
        childInit();
    }

    public abstract void childInit();

    public static void main(String args[]) {
        Child c = new Child();
        c.printValues();
    }
}

The output on a compliant JVM will be:

value1: 0                       
value2: 10                      

This is because the child's constructor is called after childInit.  I say
compliant because some non-sun compilers (cafe and possibly others) produce
different results.

thanks,

- Dale Thatcher

On Sun, Feb 11, 2001 at 09:09:13PM -0500, CPC Livelink Admin wrote:
> 
> Yes.
> 
> Have your WorkerServlets implement a procedure called childInit or
> something, which is basically an empty procedure in the SuperServlet.  Then
> call this function as the last (or first) call of the SuperServlet init
> function. This is much like how GenericServlet makes init() a convenience
> call instead of init(ServletConfig config).
> 
> Regards,
> Paul
> 
> 
> -----Original Message-----
> From: Jill Stephenson [mailto:[EMAIL PROTECTED]]
> Sent: Sunday, February 11, 2001 8:52 PM
> To: '[EMAIL PROTECTED]'
> Subject: Using a servlet superclass
> 
> 
> I have a number of servlets in my application that need to
> perform common initialisation, etc.  So I was thinking of
> creating a superclass that has an init method that does all
> of the common work, eg.,
> 
> public class SuperServlet extends HttpServlet {
>   public void init() {
>     // do common initialisation stuff in here
>   } // init
> } // SuperServlet
> 
> Then all the workers servlets would extend SuperServlet
> rather than HttpServlet, eg.,
> 
> public class WorkerServlet extends SuperServlet {
>   ...
> } // WorkerServlet
> 
> This seems to be OK, until I implement init in the
> WorkerServlet, as the init method in the SuperServlet
> does not then get invoked.  While I can call super.init()
> in the WorkServlet, I want this to be handled automatically
> as I can bet on someone forgetting this step, eg.,
> 
> public class WorkerServlet extends SuperServlet {
>   public void init() {
>     // don't want to have to make this call ==> super.init();
>   } // init
>   ...
> } // WorkerServlet
> 
> Is there any way to implement this ?
> 
> ----
> Jill
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, email: [EMAIL PROTECTED]
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, email: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]

Reply via email to