title: Function overriding in Java vs C++
body: <p>Two similar definitions in Java and C++, but totally different behaviour.</p>
  
  <p>Java version:</p>
  
  <pre><code>class base{
      public void func1(){
          func2();
      }
      public void func2(){
          System.out.println(" I am in base:func2() \n");
      }
  
  }
  
  class derived extends base{
      public void func1(){
          super.func1();
      }
      public void func2(){
          System.out.println(" I am in derived:func2() \n");
      }
  };
  public class Test
  {
      public static void main(String[] args){
          derived d = new derived();
          d.func1();
      }
  }
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>I am in derived:func2()
  </code></pre>
  
  <p>C++ version:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  class base
  {
      public:
          void func1(){
              func2();
          }
          void func2(){
              printf(" I am in base:func2() \n");
          }
  };
  
  class derived : public base
  {
      public:
          void func1(){
              base::func1();
          }
          void func2(){
              printf(" I am in derived:func2() \n");
          }
  };
  
  int main()
  {
      derived *d = new derived();
      d-&gt;func1();
      return 0;
  }
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>I am in base:func2()
  </code></pre>
  
  <hr>
  
  <p>I don't know why they have different behaviour.</p>
  
  <p>Even I know Java has auto polymorphism behaviour.</p>
  
  <p>The Java output is hard to understand personally.</p>
  
  <p><del>In my view, according to <code>static scope</code>, the base class function <code>func1()</code> should only be able to  call the base class function <code>func2()</code>, as it knows nothing about the derived class at all. Otherwise the calling behaviour belongs to <code>dynamic scope</code>.
  Maybe in C++, <code>func2()</code> in base class is bind <code>static</code>, but in Java it is bind <code>dynamic</code>?
  </del></p>
  
  <p>Member field is statically scoped.</p>
  
  <hr>
  
  <p>The type inferring part is confusing.
  I thought <code>this</code> is converted to <code>base</code> type in the <code>base::func1()</code>. In C++, the <code>base::func2()</code> is not polymorphism, so the <code>base::func1()</code> is called.
  While in Java, <code>base::func2()</code> is polymorphism, so <code>devried::func2()</code> is called.</p>
  
  <p>How the <code>func2()</code> class binding being inferred? Or
  Which <code>fun2()</code> should be called and how it is determined.</p>
  
  <p>What happened behind <code>base::func1()</code>? Is there any cast here for <code>this</code> (from <code>derive</code> to <code>base</code>)?
  If no, how <code>this</code> is able to reach to the function in <code>base</code> class?</p>
  
  <pre><code>        void func1(){
              func2();
          }
  </code></pre>
  
  <hr>
  
  <p><a href="http://www.coderanch.com/t/381808/java/java/Java-differences-function-overriding" rel="noreferrer">Useful discussion</a> on coderanch.</p>