From http://knowledgehills.com/java/java-interface-variables-default-methods.htm
We can declare variables in Java interfaces. By default, these are
public, final and static. That is they are available at all places of
the program, we can not change these values and lastly only one
instance of these variables is created, it means all classes which
implement this interface have only one copy of these variables in the
memory.
In short these are the rules for interfaces:
Member variables
- Can be only public and are by default.
- By default are static and always static
- By default are final and always final
Methods
Java 8
Form http://www.javahelps.com/2015/02/introduction-to-interface-with-java-8.html
Up to Java 1.7 version, all the methods declared in interfaces are
public and abstract by default. Since Java 1.8, an interface can have
default methods and static methods as well. Therefore, the updated rule
An interface can have default methods and static methods. Any other
methods are implicitly public and abstract. All the fields declared
in an interface are implicitly public, static and final constants.
Example:
interface Super {
//An abstract method. By default it is public and abstract.
void print();
//Default method, introduced in Java 8.
public default void doStuff() {
System.out.println("Hello world");
}
//Static method in interface, introduced in Java 8.
public static void sayHello() {
System.out.println("Hello");
}
}
Implementation:
class Base implements Super {
// Override the abstract method.
@Override
public void print() {
System.out.println("Base");
}
}