class A { public A() { foo(); } public void foo() { System.out.println("Class A"); } } class B extends A { public B(){} public void foo() { System.out.println("Class B"); } } class main { public static void main(String[] args) { A a = new B(); } } What does it print? Class A or Class B?
Sigiloso
it's absolutely Java code, no C++!! It will print ClassB, because default contructor of A will be automatically called from contructor of B (it isn't mandatory to call super default contructor from a derived default contructor). The method with the same signature overrides the base class method. If you "translate" the code to C++ without declaring as virtual the A's foo method, then it will print class A. Otherwise it will print Class B.