title: Inheritance vs Composition
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance">Prefer composition over inheritance?</a>  </p>
  </blockquote>
  
  
  
  <p>I wonder, why (or in which cases) should one consider inheritance instead of composition when there are so much cons of it:</p>
  
  <ul>
  <li>if we implement/override a method in a subclass which calls the method of the superclass, there's no guarantee that another version of our superclass (maybe some library) won't break our code</li>
  <li>if there will appear a new method in the superclass with the same signature that the sublclass method has, but with different return type, our class won't compile.</li>
  </ul>
  
  <p>Therefore, I can't imagine, how on Earth we can rely on it. The superclass author may want to improve the performance and our client code may break down.</p>
  
  <p><strong>So my questions are:</strong></p>
  
  <ul>
  <li>How are these problems solved (e.g. in standard Java libraries)?</li>
  <li>When to use inheritance and composition?</li>
  </ul>