interfaces
Before Java 8, interfaces are not allowed to have static method.
interface SS {
static void bar(); // Cannot be compiled
static void foo() {} // Cannot be compiled
}
The compiler complains: Extension methods are not supported at this language level.
But what are Extension methods? That’s a term defined in Java 8.
As for interfaces, all elements in its definition body are public:
-
all fields are public, final and static implicitly.
-
all methods are public and abstract implicitly.
-
all nested classes, interfaces are public and static implicitly, cannot be private!
interface Intfa{
// this is public and static nested class implicitly
class nestedClass {}
// this is public and static nested interface implicitly
interface nestedInterface {}
}
- static methods are not allowed. member classes or interfaces are not allowed (and has no way to declare).
An appropriate guideline is to prefer classes to interfaces. Start with classes, and if it becomes clear that interfaces are necessary, then refactor.