title: Java virtual method invocation
body: <p>Say I have the following code:</p>
  
  <pre><code>public class Employee
  {
      public int salary = 2000;
      public void getDetails() {...}
  }
  
  public class Manager extends Employee
  {
      public int salary = 5000;
      public int allowance = 8000;
      public void getDetails() {...}
  }
  </code></pre>
  
  <p>and a <code>main()</code> that does the following:</p>
  
  <pre><code>Employee emp = new Employee();
  Manager man = new Manager();
  emp.getDetails(); // method of Employee called, output ok.
  man.getDetails(); // method of Manager called, output ok.
  
  Employee emp_new = new Manager();
  emp_new.getDetails(); // method of Manager called, ok.
  System.out.println(emp_new.allowance); // problem, as Employee doesn't know about allowance. Ok
  
  // the problem
  System.out.println(emp_new.salary); // why 2000 and not 5000?
  </code></pre>
  
  <p>The book says "you get the behavior associated with the <em>object</em> to which the variable refers at runtime". Ok, I get behavior of Manager class when a <em>method</em> <code>getDetails</code> is called, but when I access an attribute <code>salary</code>, I get behavior of variable and not the object. Why is that?</p>