Java 9 Private Interface Methods

With Java 8 we got ability to implement methods in interface. From Java 8 onward you can have static methods and default methods.

Java 9 takes this a step further and introduces ability to write private implementation methods in interface. This provides greater ability for developer to control access. Private methods can be static or instance level. In both cases, the private methods are not accessible to sub-interfaces or implementation classes.


public interface SysoutLogger {

int ERROR = 0;
int WARN = 1;
int INFO = 2;
int DEBUG = 3;
int TRACE = 4;

public void trace(String message, Object[] args) {
logMessage(TRACE, null, message, args);
}

public void debug(String message, Object[] args) {
logMessage(DEBUG, null, message, args);
}

public void info(String message, Object[] args) {
logMessage(INFO, null, message, args);
}

public void warn(Throwable th, String message, Object[] args) {
logMessage(WARN, th, message, args);
}

public void error(Throwable th, String message, Object[] args) {
logMessage(ERROR, th, message, args);
}

private void logMessage(int level, Throwable th, String message, Object[] args) {
System.out.println(message);
}
}

Be the first to comment

Leave a Reply

Your email address will not be published.


*