Contact at mumbai.academics@gmail.com or 8097636691
Responsive Ads Here

Sunday, 10 June 2018

Java 9 Private methods in Interfaces


Java 9 Private methods in Interfaces


Java 9 has been released and there has been a lot of changes. Today we will look into Java 9 private methods in interfaces change.

Java 9 Private Methods in Interfaces

Java 9 private methods in interfaces
There has been a lot of changes in interfaces in recent java releases. Few days back I wrote an extensive post on Java 9 features. We will look into following sections in this post.
  1. Java 7 Interface
  2. Java 8 Interface Changes
  3. Java 9 Interface Changes
  4. Rules to define private methods in an Interface?
  5. Why do we need private methods in an Interface?
  6. Interview Questions And Answers
In this post, I have provided pseudocode only to understand this new feature because everyone knows what is an interface in java and what is a private method in Java.

Java 7 Interface

In Java SE 7 or earlier versions, an interface can have only two kinds of things.
  1. Constant variables
  2. Abstract methods
We couldn’t provide method implementations in interfaces. If we want to provide the combination of abstract methods and non-abstract methods (methods with implementation), we had to go for abstract class only.
   public interface DBLogging{
      String MONGO_DB_NAME = "ABC_Mongo_Datastore";
      String NEO4J_DB_NAME = "ABC_Neo4J_Datastore";
      String CASSANDRA_DB_NAME = "ABC_Cassandra_Datastore";

      void logInfo(String message);
      void logWarn(String message);
      void logError(String message);
      void logFatal(String message);
   }
Here we have defined couple of constants and public abstract methods. What if I would like to provide some implementation to these methods here, say default implementation. Then we should go for abstract class in Java 7 or earlier versions.

Java 8 Interface Changes

Oracle Corporation has introduced some new features to interface in Java 8 Release. That is Default methods and Static methods feature.
Yes, we can write method implementations in Interface from Java 8 onwards. We need to use defaultkeyword to define them as shown below.
In Java 8, an interface can have only four kinds of things:
  1. Constant variables
  2. Abstract methods
  3. Default methods
  4. Static methods
Default and Static methods were added in Java 8 interface changes.
   public interface DBLogging{
      String MONGO_DB_NAME = "ABC_Mongo_Datastore";
      String NEO4J_DB_NAME = "ABC_Neo4J_Datastore";
      String CASSANDRA_DB_NAME = "ABC_Cassandra_Datastore";

      // abstract method example
      void logInfo(String message);

      // default method example
      default void logWarn(String message){
         // Step 1: Connect to DataStore
         // Step 2: Log Warn Message
         // Step 3: Close the DataStore connection
      }
      default void logError(String message){
         // Step 1: Connect to DataStore
         // Step 2: Log Error Message
         // Step 3: Close the DataStore connection
      }
      default void logFatal(String message){
         // Step 1: Connect to DataStore
         // Step 2: Log Fatal Message
         // Step 3: Close the DataStore connection  
      }
      // static method example
      static boolean isNull(String str) {
 System.out.println("Interface Null Check");
 return str == null ? true : "".equals(str) ? true : false;
      }
      // Any other abstract, default, static methods
   }
Above example shows everything we can do in Java 8 interfaces, notice the extra set of default and static methods.
Did you notice the code redundancy in all the log methods? Every method is opening and closing a connection on it’s own. If we want code reuse, then we will have to move these common code to a public method, but then it will be accessible to all other classes.
What if we want to reuse the code but also don’t want to expose it to others? We will have to go for abstract class with a private method for the common code.

Java 9 Interface Changes

To provide a resolution to above scenarios, Oracle Corporation has introduced private methods in interfaces in java 9 release.
From Java 9 onwards, we can write private methods in interfaces using private access modifier as shown below (like other private methods).
In Java 9 and later versions, an interface can have six kinds of things:
  1. Constant variables
  2. Abstract methods
  3. Default methods
  4. Static methods
  5. Private methods
  6. Private Static methods
Private methods and private static methods are added in Java 9 interface changes.
public interface DBLogging {
 String MONGO_DB_NAME = "ABC_Mongo_Datastore";
 String NEO4J_DB_NAME = "ABC_Neo4J_Datastore";
 String CASSANDRA_DB_NAME = "ABC_Cassandra_Datastore";

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

 default void logWarn(String message) {
  log(message, "WARN");
 }

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

 default void logFatal(String message) {
  log(message, "FATAL");
 }

 private void log(String message, String msgPrefix) {
  // Step 1: Connect to DataStore
  // Step 2: Log Message with Prefix and styles etc.
  // Step 3: Close the DataStore connection
 }
 // Any other abstract, static, default methods
}
Here we have moved redundant code into a common private method so that our API Clients can’t see them. Notice the below image from Eclipse Oxygen supporting Java 9.
java 9 interface private methods

Rules to define private methods in an Interface?

While writing private methods in an Interface, we should follow these rules:
  1. We should use private modifier to define these methods.
  2. No private and abstract modifiers together, it will give compiler error.
  3. It is not a new rule. We cannot use the combination of private and abstract modifiers, because both have different meanings.
    • The “private” method means fully implemented method because sub-classes cannot inherit and override this method.
    • The “abstract” method means no-implementation method. Here sub-classes should inherit and override this method.
    That’s why private methods should have full implementation.
  4. Private methods must contain body.
  5. No lesser accessibility than private modifier.
  6. In Java, as we know private is the least visible access modifier. So we cannot reduce it’s visibility from private to any other modifier.
These interface private methods are useful or accessible only within that interface only. We cannot access or inherit private methods from an interface to another interface or class.

Why do we need private methods in Interface?

Java 9 private methods in interfaces have following benefits.
  1. No need to write duplicate code, hence more code reusability.
  2. We got the choice to expose only our intended methods implementations to clients.
That’s all about Java 9 private methods in interfaces change.

No comments:

Post a Comment