Loading ...
Sorry, an error occurred while loading the content.

[XP] OAOO & Inheritance

Expand Messages
  • Bryan Dollery
    Hi Guys, Just a quickie, about OAOO. Just to clarify something in my mind. I have a class, A, and direct subclasses of A: X, Y and Z. A / | (just imagine
    Message 1 of 5 , Sep 3, 2001
    • 0 Attachment
      Hi Guys,

      Just a quickie, about OAOO. Just to clarify something in my mind.

      I have a class, A, and direct subclasses of A: X, Y and Z.

      A
      / | \ (just imagine little arrows pointing up :)
      X Y Z

      A has a constructor that takes an int parameter, thus:

      public A(int value) { this.value = value; }

      where value is a private attribute of A. Did I mention this is a Java
      question.

      Okay, in the subclasses their constructor, also takes an int, and passes it
      up to the superconstructor, thus:

      public X(int value) { super(value); }
      public Y(int value) { super(value); }
      public Z(int value) { super(value); }

      appear in their respective classes. This is code duplication. How can I
      remove it so as to satisfy OAOO?

      Ta,

      Bryan




      b r y a n d o l l e r y | c e o
      c h a o s e n g i n e e r s

      021 330607
      http://www.ChaosEngineers.co.nz
    • Ilja Preuss
      ... Well, you could pass the parameter through a setter method instead of the constructor, but then the code doesn t communicate that it is needed for
      Message 2 of 5 , Sep 3, 2001
      • 0 Attachment
        > Okay, in the subclasses their constructor, also takes an int,
        > and passes it
        > up to the superconstructor, thus:
        >
        > public X(int value) { super(value); }
        > public Y(int value) { super(value); }
        > public Z(int value) { super(value); }
        >
        > appear in their respective classes. This is code duplication.
        > How can I remove it so as to satisfy OAOO?

        Well, you could pass the parameter through a setter method instead of the
        constructor, but then the code doesn't communicate that it is needed for
        construction (and possibly shouldn't be called afterwards). You could use a
        factory method to communicate this (and don't publicize the setter). But
        then how do you specify which subclass to instanciate (as static methods
        aren't polymorphic)?

        Is it worth the hassle?

        Or - to be heretical :-) - perhaps there even *is* some form of
        communication value in the duplication?

        Regards, Ilja
      • Laurent Bossavit
        ... Trick question... where do you see duplication; in the repeated calls to super() or in the repeated constructor declarations ? It seems to me that the
        Message 3 of 5 , Sep 3, 2001
        • 0 Attachment
          > public X(int value) { super(value); }
          > public Y(int value) { super(value); }
          > public Z(int value) { super(value); }
          >
          > appear in their respective classes. This is code duplication. How can I
          > remove it so as to satisfy OAOO?

          Trick question... where do you see duplication; in the repeated
          calls to super() or in the repeated constructor declarations ?

          It seems to me that the former isn't "really" duplication - or no
          more, say, than repeated uses of the reserved word "class". We're
          just saying "this object gets built the same way as its parent" - and
          saying it out loud because the language forces us to. Even well-
          factored code can't get rid of all such blemishes - see JUnit.

          We might get more mileage out of looking at the identical
          constructors as potentially "unnecessary" duplication. Why are
          there three distinct classes each of which gets built in exactly the
          same way ?

          Maybe the superclass is the only one who really needs that int,
          and the subclasses could be decorators ? You'd use something like

          X = new X().wrapping(new A(someInt));

          Maybe an A really wants to *have* an X, a Y or a Z to perform
          some variation on its functionality for it ?

          It's always a bit hard to say when not looking at real code...
        • Niels Verdonk
          ... I wouldn t call this duplication , since in constructors aren t inhereted and are in this case avoiding duplication. I would see the call to the super
          Message 4 of 5 , Sep 3, 2001
          • 0 Attachment
            > > Okay, in the subclasses their constructor, also takes an int,
            > > and passes it
            > > up to the superconstructor, thus:
            > >
            > > public X(int value) { super(value); }
            > > public Y(int value) { super(value); }
            > > public Z(int value) { super(value); }
            > >
            > > appear in their respective classes. This is code duplication.
            > > How can I remove it so as to satisfy OAOO?

            I wouldn't call this duplication , since in constructors aren't
            inhereted and are in this case avoiding duplication. I would see
            the call to the super class' constructor more syntactical limitation
            than a duplication of code.

            Niels
          • C. Keith Ray
            ... super(value) is NOT code duplication, it is the removal of code duplication (assuming that X, Y, and Z themselves are unique enough.) Look at it this way:
            Message 5 of 5 , Sep 3, 2001
            • 0 Attachment
              > Hi Guys,
              >
              > Just a quickie, about OAOO. Just to clarify something in my mind.
              >
              > I have a class, A, and direct subclasses of A: X, Y and Z.
              >
              > A
              > / | \ (just imagine little arrows pointing up :)
              > X Y Z
              >
              > A has a constructor that takes an int parameter, thus:
              >
              > public A(int value) { this.value = value; }
              >
              > where value is a private attribute of A. Did I mention this is a Java
              > question.
              >
              > Okay, in the subclasses their constructor, also takes an int, and passes it
              > up to the superconstructor, thus:
              >
              > public X(int value) { super(value); }
              > public Y(int value) { super(value); }
              > public Z(int value) { super(value); }
              >
              > appear in their respective classes. This is code duplication. How can I
              > remove it so as to satisfy OAOO?

              super(value) is NOT code duplication, it is the removal of code duplication
              (assuming that X, Y, and Z themselves are unique enough.)

              Look at it this way: putting 'back' the duplication means getting rid of the
              class 'A' and duplicating its members and methods in X, Y, and Z.

              public X(int value)
              {
              member = value;
              // other initialization that used to be in A
              }

              public Y(int value)
              {
              member = value;
              // other initialization that used to be in A
              }

              public Z(int value)
              {
              member = value;
              // other initialization that used to be in A
              }
            Your message has been successfully submitted and would be delivered to recipients shortly.