title: Late Binding in Java
body: <p>I have searched through all the similar questions on late binding on stack overflow, and I would severely disagree with anyone who marks this question as a duplicate. First off, i found this example on another question, but I do not understand how I am supposed to know when something is decided during compile time and when something is decided during run time. Basically, the crux of my question boils down to two things: </p>
  
  <ul>
  <li><p>What in this example <em>must</em> bring me to the logical conclusion that one method is late binding and another is early binding</p></li>
  <li><p>How do I know when the decision about which version of a method to execute is decided during run-time or compile time in Java</p></li>
  </ul>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>class A
  {
      public void foo()
      {
          System.out.println("Class A");
      }
  }
  
  class B extends A
  {
      public void foo()
      {
          System.out.println("Class B");
      }
  }
  
  public class C
  {
      public static void main(String [] args)
      {
          A a=new A();
          B b=new B();
          A ref=null;
  
          /* 
              early binding  --- calls method foo() of class A and
              decided at compile time
          */ 
           a.foo(); 
  
          /* early binding --- calls method foo() of class B and
              decided at compile time
          */
            b.foo(); 
  
          /* late  binding --- --- calls method foo() of class B and
             decided at Run time
       */ 
          ref=b;
          ref.foo(); }
  }
  </code></pre>