title: Difference between Dynamic and Static type assignments in Java
body: <p>Given the follow class hierarchy what are the dynamic and static types for the following statements?</p>
  
  <p><strong>Class hierarchy:</strong></p>
  
  <pre><code>class Alpha {}
  
  class Beta extends Alpha {}
  
  class Gamma extends Alpha {}
  
  class Epsilon extends Alpha{}
  
  class Fruit extends Gamma{}
  
  class Golf extends Beta {}
  
  class Orange extends Fruit{}
  </code></pre>
  
  <p>For each of the following statements, Static Type? Dynamic Type?: </p>
  
  <pre><code>Fruit f = new Fruit();
  Alpha a = f;
  Beta b = f;
  a = b;
  Gamma g = f;
  </code></pre>
  
  <p>My answers/questions <br/>
  I understand that <code>Fruit f = new Fruit()</code> will be of both static and dynamic type Fruit. <br/>
  <code>Alpha a = f;</code> Will be of type Alpha at at compile time (static) and type Fruit at runtime (dynamic).
  <br/>
  <code>Gamma g = f;</code> Will be of type Gamma at compile time (static) and type Fruit at runtime (dynamic).
  <br/>
  However I do not know the other two answers. Beta b = f is an instance in which two subclasses of the same super class are assigned to one another so I'm not sure if it would be of type Beta or type Alpha at compile time (static). And a = b is an assignment after declaration so I'm not sure what the answer for that would be. Someone please help me out thanks!</p>