+ 6

can someone explain that

What is the output of this code? public class Program { public static void main(String[] args) { B ob = new B(); } } class A { public A() { foo(); } void foo() {System.out.print(1);} } class B extends A { @Override void foo() {System.out.print(2);} } the output is 2 2-1)when creating an object the constructor of A will called A() calls foo method which should print 1 but actually it is the overridden foo when this happen ? is there any simple adjustment makes this code print 1? thanks for helping

21st Jul 2019, 10:58 PM
ABADA S
ABADA S - avatar
3 ответов
+ 3
thanks ~ swim ~ this is once do you know why this is happening here and not in c++?
22nd Jul 2019, 12:25 AM
ABADA S
ABADA S - avatar