title: Java inheritance fields
body: <p>I am not able to understand the following output.</p>
  
  <p>I don't know why the output is 10, I think the line <code>A a = new B()</code> creates a new instance of class B, I think the result should be 20</p>
  
  <pre><code>class A {
      int i = 10;
  }
  
  class B extends A {
      int i = 20;
  }
  
  public class MainClass {
      public static void main(String[] args) {
          A a = new B();
  
          System.out.println(a.i);
      }
  }
  </code></pre>
  
  <p>Why this works like this .. please explain.</p>