My question comes from a project. So let me abstract away a bit unrelated details.
I have a JAVA public class A that has two protected static methods, foo() and bar(). The method foo() calls bar() in its body.
public class A{
protected static foo(){
...
bar()
...
}
protected static bar(){print("A.bar()");}
}
Now I also have a class B extending A. In B, I override bar()
class B extends A{
@Overrides
static protected bar(){ print("A.bar() extended");
}
Finally, I call foo() from a class in B
class B extends A{
...
public static main(){foo()}
}
I cannot understand two points
1. The compiler (Eclipse) asks me to remove @Override annotation. Why?
2. Finally the main() outputs “A.bar()”, which means the resolved bar() target is of class A, but I intended to override bar() and use the A’s foo() to call the modified bar(). How can I do that?
What are your opinions?
A.foo()which in turn callsA.bar(). Since you don’t have instances, overriding methods does not work.You need to remove all the
staticfrom your code and usenew B().foo()in your main.Consider reading this tutorial.