Sitemap
Nuwan Zen

Nuwan Zen

Sometimes A software Engineer, sometimes a support engineer, sometimes a devops engineer, sometimes a cloud engineer :D That’s how the this life goes!

Java9: Private Interface Methods

Mar 21, 2021

Java9 allows to have private interface methods so that the default methods can use them reduce repeating codes.

Press enter or click to view image in full size
Photo by the blowup on Unsplash
public class PrivateInterfaceMethods {
public static void main(String[] args) {
OracleLogger oracleLogger = new OracleLogger();
oracleLogger.logError("error in oracle connection");
oracleLogger.logInfo("Connection established");
}
}

interface Logging{
String ORACLE = "Oracle_Database";
String MYSQL = "MySql_Database";

default void logInfo(String message) {
log(message, "INFO");
}
default void logError(String message) {
log(message, "ERROR");
}
private void log(String message, String prefix){
System.out.println("Log Message : " + prefix);
}
}

final class OracleLogger implements Logging {}
final class MySqlLogger implements Logging {}

private log() method is used by both default methods to reduce repeating.

Get Nuwan Zen’s stories in your inbox

Join Medium for free to get updates from this writer.

Read my other blogs:

--

--

Nuwan Zen
Nuwan Zen

Written by Nuwan Zen

Sometimes A software Engineer, sometimes a support engineer, sometimes a devops engineer, sometimes a cloud engineer :D That’s how the this life goes!