title: How Does the JVM handle Dynamic Dispatch in this Situation?
body: <p>Given the following source and ouput:</p>
  
  <p>Source:</p>
  
  <pre><code>public class A
  {
    public void foo()
    {
      bar();
    }
  
    public void bar()
    {
      System.out.println ("in A's bar() method");
    }
  }
  
  public class B extends A
  {
    @Override
    public void foo()
    {
      super.foo();
  
      // Do some specialized B stuff
    }
  
    @Override
    public void bar()
    {
      System.out.println ("in B's bar() method");
    }
  }
  
  public class Main
  {
    public static void main (String... args)
    {
      B b = new B();
  
      b.foo();
    }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <p><code>in B's bar() method</code></p>
  
  <p>Can someone please explain to me how the JVM is smart enough to polymorphically call B's (as  opposed to A's) bar() method in this situation? I'd like to know what kind of dynamic dispatch magic is going on behind the scenes here.</p>
  
  <p><strong>Update</strong>: In case I wasn't clear enough, I know basically WHAT is happening, I'm looking for specific details on HOW the JVM makes it happen under the hood. The answers so far are too simplistic.</p>
  
  <p><strong>Update 2</strong>: Maybe I wasn't clear enough. When <code>b.foo()</code> is called, then <code>super.foo()</code> is called, then <code>bar()</code> is called in class A's <code>foo()</code>. How does the <code>bar()</code> that is called when specifically invoking <code>super.foo()</code> not call class A's <code>bar()</code> method, since the <code>super</code> keyword explicitly specifies class A? What steps does the JVM have to go through to sort this out?</p>
  
  <p>Also, does this mean it's a bad idea in general to call public methods from within their own class since they can be overridden in this way?</p>