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)?

The actual code that provoked me to find this
bug goes something like this below. The problem
is with the `user' variable, since `receiveBody'
is called before this variable has been constructed.
I've since been able to rework the code (using
a static method in the Message* subclasses to
act as a factory for those message types) and
it works better now.

Rich.

/* Message.java */

public abstract class Message
{
  protected Message (DataInputStream input, int type)
    throws MessageProtocolError, IOException
  {
    int streamType = input.readInt ();
    if (streamType != type)
      throw new MessageProtocolError ("incorrect message type " +
                                        streamType +
                                        " (expecting " +
                                        type +
                                        ")");
    receiveBody (input);
  }

  protected abstract void receiveBody (DataInputStream input)
    throws IOException, MessageProtocolError;

}

/* Lots of Message* classes implementing particular
 * message types, of which one looks like this:
 */

public class MessageUserLogin extends Message
{
  public String user /* = null */;
  private static final int type = MessageType.UserLoginType;

  /**
   * Construct message from input stream of known type.
   */
  public MessageUserLogin (DataInputStream input)
    throws MessageProtocolError, IOException
  {
    super (input, type);
  }

  /**
   * Receive the message.
   */
  protected void receiveBody (DataInputStream input)
    throws IOException
  {
    this.user = input.readUTF ();
  }
}

-- 
-      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.    -

Reply via email to