I know it is a pretty beaten topic here but there is something i need to clarify, so bear with me for a minute.
Static methods are inherited just as any other method and follow the same inheritance rules about access modifiers (private methods are not inherited etc.)
Static methods are not over-ridden they are redefined. If a subclass defines a static method with the same signature as one in the superclass, it is said to be shadowing or hiding the superclass's version not over-riding it since they are not polymorphic as instance methods.
The redefined static methods still seem to be following some of (if not all) of the over-riding rules.
Firstly, the re-defined static method cannot be more access restricted than the superclass's static method. Why??
Secondly, the return types must also be compatible in both the superclass's and subclass's method. For example:
class Test2 {
static void show() {
System.out.println("Test2 static show");
}
}
public class StaticTest extends Test2 {
static StaticTest show() {
System.out.println("StaticTest static show");
return new StaticTest();
}
public static void main(String[] args) {
}
}
In eclipse it shows an error at line at:
The return type is incompatible with Test2.show()
Why??
And Thirdly, are there any other rules that are followed in re-defining static methods which are same as the rules for over-riding, and what is the reason for those rules?
Thanx in advance!!