title: Make Method Re-usable
body: <p>I asked a similar question earlier today, but now I am told that the requirements have changed.</p>
  
  <p>I have 3 classes - <code>Product</code>, <code>ProductOffering</code> (PO) and <code>ProductOfferingLists</code> (POL).  Product contains PO, and PO contains POL.  All those classes extend an abstract class called CMS.</p>
  
  <p>I want to attach PO to P, and POL to PO, but I am looking for a re-usable method, since the logic is identical, except for the class names.</p>
  
  <p>Unfortunately, my tester class is requiring two methods and the hierarchy classes are requiring a cast to fulfill the abstract CMS contract.</p>
  
  <p>This is driving me nuts.  How can I make this more reusable so I don't have multiple methods and casting?</p>
  
  <pre><code>package puzzler;
  import java.util.ArrayList;
  import java.util.List;
  
  public class Tester {
      public static void main(String[] args) {
          Product p = new Product();
          PO pa = new PO();
          POL po = new POL();
  
          List&lt;PO&gt; lpa = new ArrayList&lt;PO&gt;();
          List&lt;POL&gt; lpo = new ArrayList&lt;POL&gt;();
  
          attachChildToParent(lpa, p);
          attachChildToParent(lpo, pa);
      }
  
      static void attachChildToParent(List&lt;PO&gt; listChild, Product parent) {
          for (PO po : listChild) {
              parent.attach(po);
          }
      }
  
      static void attachChildToParent(List&lt;POL&gt; listChild, PO parent) {
          for (POL po : listChild) {
              parent.attach(po);
          }
      }
  }
  </code></pre>
  
  <p>The CMS Class:</p>
  
  <pre><code>package puzzler;
  
  abstract class CMS {
      abstract void attach(CMS childNode);
  }
  </code></pre>
  
  <p>Product Class - the top of the hierarchy that owns PO:</p>
  
  <pre><code>package puzzler;
  import java.util.List;
  
  public class Product extends CMS {
      List&lt;PO&gt; lpo;
  
      public List&lt;PO&gt; getLpo() {
          return lpo;
      }
  
      public void setLpo(List&lt;PO&gt; lpo) {
          this.lpo = lpo;
      }
  
      @Override
      void attach(CMS childNode) {
          this.getLpo().add((PO)childNode);
      }
  }
  </code></pre>
  
  <p>Product Offering, owned by Product and in turn owns a Product Offering List </p>
  
  <pre><code>package puzzler;
  import java.util.List;
  
  public class PO extends CMS {
      List&lt;POL&gt; lpol;
  
      public List&lt;POL&gt; getLpo() {
          return lpol;
      }
  
      public void setLpo(List&lt;POL&gt; lpol) {
          this.lpol = lpol;
      }
  
  
      @Override
      void attach(CMS childNode) {
          this.getLpo().add((POL)childNode);
      }
  }    
  </code></pre>
  
  <p>Product Offering List (POL) owned by PO</p>
  
  <pre><code>package puzzler;
  
  public class POL extends CMS {
      @Override
      void attach(CMS childNode) {
          throw new UnsupportedOperationException("Not supported yet.");
      }
  }
  </code></pre>