I apologize if I am duplicating a question. I am struggling to understand something. Any help would be appreciated! We have 3 classes, A, B and C as follows:
public class A {
public void func() {System.out.println("A's func");}
}
public class B extends A{
private void f() {System.out.println("B's f");}
public void foo() {
f();
func();
}
}
public class C extends B {
public void f() {System.out.println("C's f");}
public void func() {System.out.println("C's func");}
public static void main(String[] args) {
B b = new C();
b.foo();
}
}
C's main will print:
B's f
C's func
And I am struggling to understand why the output isn't:
C's f
C's func
Since foo doesn't exist in C, B's foo method is invoked. But then it calls f. As I see it, b's dynamic type is C, so why isn't C's f method isn't invoked? I figured that it has to do something with visibility of B's f method. It's private, and so it's invoked relative to the static type of b. So what I am not getting here, why B's method got the precedence? After all, when it invoked the func method in foo, it did call C's funct method...
Thank you!