With this code:
public abstract class A {
private String runNow() {
return "High";
}
public abstract String cos();
static class B extends A {
public String runNow() {
return "Low";
}
public String cos() {
return "cos from B";
}
}
public static void main(String args[]) {
A[] a = new B[] {new B(), new C()};
for (A aa : a) {
System.out.println(aa.runNow() + " " + aa.cos());
}
}
}
class C extends A.B {
public String runNow() {
return "Out";
}
public String cos() {
return "cos from C";
}
}
Why is the runNow
method from class A
invoked and not from the subclasses?