Java 9 private interface methods
In Java SE 9 interfaces can have private and private static methods.
It was planned to have them yet in Java 8 SE, but was postponed due to other high priority features.
What’s the purpose of having private methods in interface? As we know in Java 8 interfaces can have default methods, so if between default methods needs to be shared code, private method can be used. Let’s see an example:
public interface Foo {
default int max(List input) {
List sorted = sort(input);
return sorted.get(sorted.size() - 1);
}
default int min(List input) {
List sorted = sort(input);
return sorted.get(0);
}
private List sort(List input) {
Collections.sort(input);
return input;
}
}