2 Replies Latest reply: Apr 7, 2010 2:26 PM by 843853 RSS

    Inherit, simple OO issue ...

    843853
      Hi,

      I can't find a solution for this simple OO problem :

      I have a class NioClient and a class NioClientAdmin extending NioClient (adding some extra functionalities for admin users).
      In the constructor of NioClientAdmin, I call super (parent constructor) with the arguments to initialize and inherit all of the variables. However, NioClientAdmin should override some variables, let me show you that by some few lines of code. In the code below when I call super, I don't want to create a CommandsDirectory instance but a CommandsDirectoryAdmin instance. In the sample below I will create a CommandsDirectory for nothing ...

      public class NioClient implements Runnable {
      protected User user;
      protected int port;
      // and many other variables
      private CommandsDirectoryAbstract directory;

      public NioClient(port, user) {
      this.user = user;
      this.port = port;
      // and other variables
      directory = new CommandsDirectory.getInstance().init();
      }
      [...]

      public class NioClientAdmin extends NioClient {
      private CommandsDirectoryAbstract directory;

      public NioClient(int port, User user) {
      super(port, user); // call parent's constructor and thus create a CommandsDirectory object
      directory = new CommandsDirectoryAdmin.getInstance().init(); // create the CommandsDirectoryAdmin object
      }
      [...]

      I hope I was clear enough,
      Thank you for any help