3 Replies Latest reply: Apr 9, 2010 10:24 AM by 843853 RSS

    Alternative of multiple inheritance in this case ...

    843853
      Hi,

      I have a situation where I cannot think of a good design without multiple inheritance, which isn't allowed by Java :
      The structure is shown on the image in the link below.

      I have classes NioClient and IoClient which both may handle Sockets. Those classes are extending a Client class which is an abstract class containing the common code for the concrete classes.

      In the same way I have NioClientAdmin and IoClientAdmin extending a ClientAdmin containing common code. Those Admin classes are adding extra functionnalities to the default classes. That's why all the Admin classes must extend the associated default classes.

      http://imagepaste.nullnetwork.net/viewimage.php?id=760

      Also, the Admin classes are in a different project because I must be able to export the default classes without the Admin classes for default users.

      Thank you for any tips ...
        • 1. Re: Alternative of multiple inheritance in this case ...
          800387
          You should be able to use composition and delegation rather than inheritance to solve your issue.

          - Saish
          • 2. Re: Alternative of multiple inheritance in this case ...
            796262
            Instead of thinking that a Client is an Admin (inheritance), think that a Client has some AdminRights (composition).

            So instead of doing something like this:
            this.doAdminThingOne();
            You would do this:
            myAdminRights.doAdminThingOne();
            • 3. Re: Alternative of multiple inheritance in this case ...
              843853
              mathiii wrote:
              Hi,

              I have a situation where I cannot think of a good design without multiple inheritance, which isn't allowed by Java :
              The structure is shown on the image in the link below.

              I have classes NioClient and IoClient which both may handle Sockets. Those classes are extending a Client class which is an abstract class containing the common code for the concrete classes.

              In the same way I have NioClientAdmin and IoClientAdmin extending a ClientAdmin containing common code. Those Admin classes are adding extra functionnalities to the default classes. That's why all the Admin classes must extend the associated default classes.
              It sounds like you're using inheritance to simply re-use some code, rather than because it actually models the domain. That's often the cause of awkwardness.