title: Avoid casting in inherited java-classes
body: <p>I have a class:</p>
  
  <pre><code>class MyClass {
  
    public MyClass getParent() { ... }
  
    public MyClass[] getChildren() { ... }
  
    ....
  
  }
  </code></pre>
  
  <p>and a subclass</p>
  
  <pre><code>MySubClass extends MyClass {
  
    public String getId() { }
  
    ...
  }
  </code></pre>
  
  <p>Everytime I used <code>getChildren()</code> or <code>getParent()</code> on an instance of <code>MySubClass</code>, I have to cast the result of theese methods, e.g.:</p>
  
  <pre><code>MySubClass sub = new MySubClass();
  ((MySubClass)sub.getParent()).getId();
  </code></pre>
  
  <p>Is there any way (by language or design) to avoid that casting? </p>
  
  <p>Thanks for any ideas!</p>
  
  <p><strong>Update</strong>
  What I would like is that <code>getParent()</code> and <code>getChildren()</code> always return the type of the instance they are called from, e.g. <code>sub.getChildren()</code> should return <code>MySubClass[]</code></p>