title: abstract class and common code
body: <p>I have a question about abstract classes and their real purpose.</p>
  
  <p>Consider the below scenario:</p>
  
  <pre><code>interface A {
      void execute();
  }
  
  class AOne implements A {
      public void execute() {
          x = getX();
          ..
          functionality specific to A
          ..
          y = getY();
          ..
          more funtionality specific to A
      }
  
      private X getX() {
          .. 
          return x;
      }
  
      private Y getY() {
          ..
          return y;
      }
  }
  
  class ATwo implements A {
      public void execute() {
          x = getX();
          ..
          functionality specific to B
          ..
          y = getY();
          ..
          more funtionality specific to B
      }
  
      private X getX() {
          ..
          return x;
      }
  
      private Y getY() {
          ..
          return y;
      }   
  }
  </code></pre>
  
  <p>So, I have an interface A which declares a method execute() and 2 implementations, AOne and ATwo both of which implement/define execute()
  You will notice that the execute methods in both implementations have some common functionality i.e. method calls getX() and getY(). These methods in both implementations do the same thing i.e. they are duplicated in both the subclasses.</p>
  
  <p>Now, to the problem. I will make some modifications to the above code by implementing an abstract class in between the interface and the implementations. </p>
  
  <pre><code>interface A {
      void execute();
  }
  
  public abstract class AbstractA implements A {
      protected X getX() {
          .. 
          return x;
      }
  
      protected Y getY() {
          ..
          return y;
      }
  }
  
  class AOne extends AbstractA {
      public void execute() {
          x = getX();
          ..
          functionality specific to A
          ..
          y = getY();
          ..
          more funtionality specific to A
      }
  }
  
  class ATwo extends AbstractA {
      public void execute() {
          x = getX();
          ..
          functionality specific to B
          ..
          y = getY();
          ..
          more funtionality specific to B
      }
  }
  </code></pre>
  
  <p>You will notice now that the methods earlier duplicated in the implementation classes AOne and ATwo i.e. getX() and getY() have been moved to the abstract class AbstractA. Also notice that AbstractA implements A but does not mention any implementation for execute(). It just holds the code that is common to AOne and ATwo</p>
  
  <p>Now, I think the above is wrong use of an abstract class and incorrect object oriented programming. Abstract classes shouldnt be used to hold code common to implementing classes, atleast not in this manner.</p>
  
  <p>Could someone please shed some light on this and let me know if the above is correct or not. If it is, why and if it is not why?</p>
  
  <p><strong>Edit:</strong> I believe I got the "common code" part wrong. I agree that abstract classes are used to hold functionality common to various implementing classes. But if we consider that methods getX() and getY() are some kind of utilities that both AOne and ATwo classes need, is it still a good idea to pull them up to the abstract class?</p>
  
  <p>The bottom line of the question being: <strong>should abstract classes be used to hold common utility code from the subclasses?</strong></p>