title: What happens when base and derived classes each have variables with the same name
body: <p>Consider the <code>int a</code> variables in these classes:</p>
  
  <pre><code>class Foo {
      public int a = 3;
      public void addFive() { a += 5; System.out.print("f "); }
  }
  class Bar extends Foo {
      public int a = 8;
      public void addFive() { this.a += 5; System.out.print("b " ); }
  }
  public class test {
      public static void main(String [] args){
          Foo f = new Bar();
          f.addFive();
          System.out.println(f.a);
      }
  }
  </code></pre>
  
  <p>I understand that the method <code>addFive()</code> have been overridden in the child class, and in class test when the base class reference referring to child class is used to call the overridden method, the child class version of <code>addFive</code> is called.</p>
  
  <p>But what about the public instance variable <code>a</code>? What happens when both base class and derived class have the same variable?</p>
  
  <p>The output of the above program is </p>
  
  <pre><code>b 3 
  </code></pre>
  
  <p>How does this happen?</p>