title: Subclass references another subclass, accessible from the same getter/setter
body: <p>I am trying to achieve an OOP inheritance design in Java where, if I am presented with a subclass of <code>OrderTypeA</code>, and calling its <code>getOrderLines()</code>, it will return a collection of type <code>OrderLineTypeA</code>. Likewise, if I have an instance of <code>Order</code>, its <code>getOrderLines()</code> will return a collection of instance <code>Collection&lt;OrderLine&gt;</code></p>
  
  <p>I have tried the following, which does not seem to compile.</p>
  
  <p>What I am trying to achieve:</p>
  
  <p><strong>Proposed Architecture:</strong>
  <a href="https://i.stack.imgur.com/vC1Ei.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vC1Ei.png" alt=""></a></p>
  
  <p>Order</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Order {
      protected List&lt;? extends OrderLine&gt; orderLines;
  
      public List&lt;? extends OrderLine&gt; getOrderLines() {
          return orderLines;
      }
  
      public void setOrderLines(List&lt;OrderLine&gt; orderLines) {
          this.orderLines = orderLines;
      }
  }
  </code></pre>
  
  <p>OrderLine</p>
  
  <pre class="lang-java prettyprint-override"><code>public class OrderLine {
      private Order order;
  
      public Order getOrder() {
          return order;
      }
  
      public void setOrder(Order order) {
          this.order = order;
      }
  }
  </code></pre>
  
  <p>OrderTypeA</p>
  
  <pre class="lang-java prettyprint-override"><code>public class OrderTypeA extends Order {
      protected List&lt;OrderLineTypeA&gt; orderLines;
  
      @Override
      public List&lt;OrderLineTypeA&gt; getOrderLines() {
          return orderLines;
      }
  
      @Override
      public void setOrderLines(List&lt;OrderLine&gt; orderLines) {
          this.orderLines = orderLines; // does not compile
      }
  }
  </code></pre>
  
  <p>OrderLineTypeA</p>
  
  <pre class="lang-java prettyprint-override"><code>public class OrderLineTypeA extends OrderLine {
      private OrderTypeA order;
  
      @Override
      public Order getOrder() {
          return order;
      }
  
      @Override // does not compile, technically not an override
      public void setOrder(OrderTypeA order) {
          this.order = order;
      }
  }
  </code></pre>
  
  <p>Other solutions I have tried, includes omitting field declarations in the child classes, in order to hide the field in the parent. </p>
  
  <p>Other ideas include overriding methods in the child, and casting return statements to the type of the child.</p>
  
  <p>Thanks!</p>