Consider the following classes:
public class A {
public void foo() {
System.out.println("A.foo()");
}
public void bar() {
System.out.println("A.bar()");
foo();
}
}
public class B extends A {
public void foo() {
System.out.println("B.foo()");
}
public static void main(String[]
args) {
A a = new B();
a.bar();
}
}
The output of this code is A.bar()
and then B.foo()
. I've noticed that if I change the method foo()'s access level from public
to private
the output is: A.bar()
and then A.foo()
.
Why?