Java static methods can be overridden but without any use because it calls the base class method only. So what is the use of defining a static method in an interface?
public interface Foo {
public static int bar() {
...
}
}
1.The interface can not be instantiated
2.Even if it doesn't shows any error while inheriting it finds no practical use:
Ex:
class Base {
// Static method in base class which will be hidden in subclass
public static void display() {
System.out.println("Static or class method from Base");
}
// Non-static method which will be overridden in derived class
public void print() {
System.out.println("Non-static or Instance method from Base");
}
}
// Subclass
class Derived extends Base {
// This method hides display() in Base
public static void display() {
System.out.println("Static or class method from Derived");
}
// This method overrides print() in Base
public void print() {
System.out.println("Non-static or Instance method from Derived");
}
}
Output:
Static or class method from Base
Non-static or Instance method from Derived