title: Calling an overridden method from a parent class ctor
body: <p>I tried calling an overridden method from a constructor of a parent class and noticed different behavior across languages.</p>
  
  <p><code>C++</code> -  <strong>echoes <code>A.foo()</code></strong> </p>
  
  <pre><code>class A{
  
  public: 
  
      A(){foo();}
  
      virtual void foo(){cout&lt;&lt;"A.foo()";}
  };
  
  class B : public A{
  
  public:
  
      B(){}
  
      void foo(){cout&lt;&lt;"B.foo()";}
  };
  
  int main(){
  
      B *b = new B(); 
  }
  </code></pre>
  
  <p><code>Java</code> - <strong>echoes <code>B.foo()</code></strong></p>
  
  <pre><code>class A{
  
      public A(){foo();}
  
      public void foo(){System.out.println("A.foo()");}
  }
  
  class B extends A{  
  
      public void foo(){System.out.println("B.foo()");}
  }
  
  class Demo{
  
      public static void main(String args[]){
          B b = new B();
      }
  }
  </code></pre>
  
  <p><code>C#</code> - <strong>echoes <code>B.foo()</code></strong></p>
  
  <pre><code>class A{
  
      public A(){foo();}
  
      public virtual void foo(){Console.WriteLine("A.foo()");}
  }
  
  class B : A{    
  
      public override void foo(){Console.WriteLine("B.foo()");}
  }
  
  
  class MainClass
  {
      public static void Main (string[] args)
      {
          B b = new B();              
      }
  }
  </code></pre>
  
  <p>I realize that in C++ objects are created from top-most parent going down the hierarchy, so when the constructor calls the overridden method, B does not even exist, so it calls the A' version of the method. However, I am not sure why I am getting different behavior in Java and C# (from C++)</p>