title: How does Java pick which overloaded function to call?
body: <p>This is a purely theoretical question.</p>
  
  <p>Given three simple classes:</p>
  
  <pre><code>class Base {
  }
  
  class Sub extends Base {
  }    
  
  class SubSub extends Sub {
  }
  </code></pre>
  
  <p>And a function meant to operate on these classes:</p>
  
  <pre><code>public static void doSomething(Base b) {
    System.out.println("BASE CALLED");
  }
  public static void doSomething(Sub b) {
    System.out.println("SUB CALLED");
  }
  </code></pre>
  
  <p>It seems that the followign code:</p>
  
  <pre><code>SubSub ss = new SubSub();
  doSomething(ss);
  </code></pre>
  
  <p>could legitimately result in printing either BASE CALLED, or SUB CALLED, since SubSub can be casted to both of those. In fact, removing the Sub version of the function causes BASE CALLED to be printed. What actually happens is that "SUB CALLED" is printed. This seems to mean that which function is called doesn't depend on the order the functions are defined in, as the Base version was called first. </p>
  
  <p>Does Java just look at all the different versions of the function and pick the one which requires the smallest traversal up the inheritance stack? Is this standardized? Is it written out in any documentation?</p>