Saturday 14 January 2017

Java9 : private methods in interfaces

From Java9 onwards, interfaces allow private method definitions. Prior to Java7, interfaces are very simple, they contain only abstract methods. Java8 changes this behaviour by providing default implementation for interface methods and also provides the support for static methods in interfaces.

Now Java9 takes this one step forward by allowing the private methods in interfaces to achieve code reusability.

Factorial.java
public interface Factorial{

 public default int factorial(int n){
  if (n < 0){
   throw new IllegalArgumentException("Number should not be negative");
  }
  
  return fact(n);
 }
 
 private int fact(int n){
  if(n == 0 || n == 1){
   return 1;
  }
  
  return n * fact(n-1);
 }
 
}

Now from Java9 on wards methods can be private, public (or) default (Remember if you don’t provide any access specifier to a method, interface takes it as public by default).

Can I apply private keyword on both static and instance methods?
Yes, you can apply private access specifier on both static and instance methods.

Test.java
public interface Test{

 public static int factorial(int n){
  if (n < 0){
   throw new IllegalArgumentException("Number should not be negative");
  }
  
  return fact(n);
 }
 
 private static int fact(int n){
  if(n == 0 || n == 1){
   return 1;
  }
  
  return n * fact(n-1);
 }
}

Following list summarises the valid combinations of private, public, static, abstract and default.

public static : Valid Combination
public abstract : Valid Combination
public default : Valid Combination
private static : Valid Combination

Following table summarises the invalid combinations.

Combination
Compiler Error
static default
illegal combination of modifiers: static and default
private abstract
illegal combination of modifiers: abstract and private
private default
illegal combination of modifiers: abstract and private

Why do we require private methods in interfaces?
If two default methods needed to share code, a private interface method would allow them to do so, but without exposing that private method and all its "implementation details" via the interface.

Test.java

public interface Test{

 default String getNameInLowerCase(Employee emp){
  String name =  concatAndLowerStrings(emp.firstName, emp.lastName);
  return "[" + name + "]";
 }
 
 default String getEmployeeDetails(Employee emp){
  String name = concatAndLowerStrings(emp.firstName, emp.lastName);
  return "[" + emp.id + " , " + name + "]";
 }
 
 private String concatAndLowerStrings(String str1, String str2){
  String str3 = str1 +" , " +str2;
  return str3.toLowerCase();
 }
 
 public class Employee{
  public String firstName;
  public String lastName;
  public int id;
  
 }
}

Notify above snippet, methods ‘getNameInLowerCase’, ‘getEmployeeDetails’ use concatAndLowerStrings method without exposing the method details using private keyword.

References





Previous                                                 Next                                                 Home

No comments:

Post a Comment