Private Methods In Interfaces In Java 9



Java SE 9 is nearing its release date. At this time we have a fair idea about new features in Java 9. One of the major enhancements is the introduction of Private Methods in Interfaces. Initially, it might be confusing to understand requirement of private methods in an Interface, but let's try to understand it step by step.

Pre Java 8 Era

In pre Java 8 era, Interfaces were only skeletons to define class structure. They were not allowed to have definition for any method.
public interface DBManager{

      String USER_NAME = "xyz";

      String PASSWORD = "*********";


      void save(Employee obj);

      void save(Department obj);

}
This created a situation, where, if we had single behavior for a method, we had to repeat same piece of code in each implementing class. This resulted in lot of redundant code. Another solution was, to use Abstract Class instead of an Interface. This approach had its own obvious problems.

Default Methods in Java 8

Java 8 solved this problem by allowing Interfaces to have default implementation for a method, these were called default methods. A default method had to be marked with default keyword. We can choose not to provide implementation of default methods in implementing Classes.
public interface DBManager{

      String USER_NAME = "xyz";

      String PASSWORD = "*********";


      default void save(Employee obj){

  //1. create connection

  //2. save object.

  //3. close connection

      }
  

      default void save(Department obj){

  //1. create connection

  //2. save object.

  //3. close connection

      }

}
This is a great solution to problems of Java 7 and earlier. But it came with its own drawback. What if we have multiple default methods and there is a code redundancy between these methods? Of course, we can extract the redundant code to a separate default method. But then that method becomes public and can be accessed directly by external classes, instead of being accessed through initial default methods.


Private Methods in Java 9

Private methods in Interfaces in Java 9, come as a required solution to problem of code redundancy in default methods in Java 8.
public interface DBManager{


      String USER_NAME = "xyz";

      String PASSWORD = "*********";


      default void save(Employee obj){

  saveToDb(obj, Employee.class);

      }


      default void save(Department obj){

  saveToDb(obj, Department.class);

      }


     private void saveToDb(Object obj, Class type){

  //1. create connection

  //2. save object with type.

  //3. close connection
     }


}


Now in Java 9, we can extract common code between default methods of an Interface into a private method. The Private Methods of an Interface are not accessible out side an Interface, neither are they inherited by implementing classes.

I hope I have been able to explain the feature and use of Private Methods in Interfaces in Java 9.

Please leave your feedback in comments below.


0 comments:

Post a Comment