title: Question about Java polymorphism and casting
body: <p>I have a class C. Class E extends it.</p>
  
  <pre><code>E e = new E();
  C c = new C();
  </code></pre>
  
  <p>Why is</p>
  
  <pre><code>e = (E) c;
  </code></pre>
  
  <p>Upon further review: though numeric conversions have the same syntax as casting objects, some confusion arose. At any event, the above does not give a compilation, but rather a runtime error - so a class can be casted to subclass in some instances (otherwise the code would not compile). Any examples that anyone can give where the above works?</p>
  
  <p>And also:</p>
  
  <pre><code>K extends M
  
  K k = new K();
  </code></pre>
  
  <p><code>((M) k).getClass()</code> gives <code>K</code>. Why is that? It was casted to the more general <code>M</code>!</p>
  
  <p>Suppose I have a doIt() method implemented in both M and K. executing</p>
  
  <pre><code>((M) k).doIt();
  </code></pre>
  
  <p>gives M's or K's doIt()?</p>
  
  <p>Thanks!</p>