Consider the below code snippet
It prints 1
But if f() is not private then it invokes the method in PolyC and prints 2
Since PolyC does not have g() method the inherited method from PolyB is invoked.Now it has to resolve the call to f().
My doubt is since it will call this.f() why is not calling f() method in polyC than private f() method in PolyB...I am confused ..Is it some thing to do with executing within the context
public class
test {
public static void main(
String args[]) {
PolyA ref1 = new PolyC();
PolyB ref2 = (PolyB)ref1;
System.out.println(ref2.g()); // This prints 1
// If f() is not private in PolyB, then prints 2
}
}
class PolyA {
private int f() { return 0; }
public int g() { return f(); }
}
class PolyB extends PolyA {
private int f() { return 1; }
public int g() { return f(); }
}
class PolyC extends PolyB {
public int f() { return 2; }
}