BIG
DATA

JAVA

Private Interface Methods in Java 9

Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm
 

JEP 213: Milling Project Coin

One of the item from this JEP(JDK Enhancement Proposal) is bring in support for private methods in interfaces. Support for private methods in interfaces was briefly in consideration for inclusion in Java SE 8 as part of the effort to add support for Lambda Expressions, but was withdrawn to enable better focus on higher priority tasks for Java SE 8. But for Java SE 9 it is now proposed that support for private interface methods be undertaken thereby enabling non abstract methods of an interface to share code between them.


Interfaces in Java 7 or earlier

In Java 7 and all earlier versions, interfaces were simple. They could only contain public abstract methods.So an interface can have only two kinds of things in Java SE 7 or earlier versions:

  • Constant variables
  • Abstract methods

You can't provide method implementations in interfaces but if you want to provide the combination of abstract methods and non-abstract methods, you should go for Abstract class.

public interface DBLogging{
  public String DB_NAME = "Log_DB";
  public void logInfo(String message);
  public void logError(String message);
  // Any other abstract methods
}

Interfaces in Java 8

In java 8 Default methods and Static methods feature was introduced by Oracle Corporation. So yes, we can write method implementations in Interface from Java SE 8 and on-wards. We need to use “default” keyword to define them.

In Java SE 8 and later versions, an interface can have only four kinds of things:

  • Constant variables
  • Abstract methods
  • Default methods
  • Static methods
public interface DBLogging{
  String DB_NAME = "Log_DB";

  default void logInfo(String message){
     //Connect to Log DB
     //Log Info Message
     //Close the DB connection
  }
 
  default void logError(String message){
     //Connect to DataStore
     //Log Error Message
     //Close the DB connection
  }
  // Any other abstract methods
}

So did you observe something from the above code. Yes you guessed it correctly, there is redundant code. If we want to extract that redundant code to common method, that should be public method as private method is not allowed. But that exposes one more method which API developer is not interested. How do we solve this? Yes by having private methods, thats exactly what Java 9 has introduced to interfaces.

Interfaces in Java 9

In Java SE 9, we can write private methods in Interfaces using ‘private’ access modifier like other private methods. Hence an interface can have below:

  • Constant variables
  • Abstract methods
  • Default methods
  • Static methods
  • Private methods
  • Private Static methods
public interface DBLogging{
  String DB_NAME = "Log_DB";

  default void logInfo(String message){
    log(message, "INFO")
  }
  default void logError(String message){
     log(message, "ERROR")
  }

  private void log(String message, String messagePrefix){
     //Connect to DB
     //Log Message
     //Close the DB connection  
  }
  // Any other abstract methods
}

Here I have extracted redundant code into a common private method so that our API Clients cannot see them.

Benefits of having private methods

  • No need to write Duplicate Code i.e. Code Reusability.
  • We can expose only our intended methods to clients.