May 18, 2022

Private Methods in Java Interface

In Java 8 two new features were added to the interfaces in Java- Default methods and static methods. Java 9 added one more capability of adding private methods to the interfaces in Java.

Interface private method in Java

Private methods in Java interfaces can be defined using private modifier the same way it is done for a Java class.

private methodName(arguments){
  ..
  ..
}

You can add both private methods and private static methods in interfaces, Java 9 onward.

Why do we need private methods in interfaces

With the inclusion of default and static methods one problem cropped up in interfaces. What if there is a common code in default methods or static methods? There was no proper way to keep that common code in a separate method without exposing it to the implementing classes. You could only keep the common code in another default or static method but by default all the methods in interfaces were public thus exposing any such code to the implementing classes.

Consider the following scenario which would have been the case in Java 8-

public interface MyInterface {
  default void method1(){
    commonCode();
    System.out.println("In method 1");
  }

  default void method2(){
    commonCode();
    System.out.println("In method 2");
  }

  default void commonCode(){
    System.out.println("Executing common code...");
  }
}

Here we have two default methods method1() and method2() in the interface. There is some common code executed by both the methods, so it is kept in a separate default method to avoid duplicate code.

But any class implementing this interface could access commonCode() method too as all the methods were public by default till Java 8. Another option was to keep the same code  in both of the methods thus having code redundancy.

public class MyClassImpl implements MyInterface {
  public static void main(String[] args) { 
    MyClassImpl obj = new MyClassImpl();
    obj.commonCode();
  }
}

With the inclusion of private methods in Java interfaces from Java 9 onward you can keep such code in a private method which is visible only with in the interface. That increases code reusability by avoiding code redundancy and also keeps the code safe by keeping it private.

Same interface with commonCode() as private method (possible from Java 9 onward).

public interface MyInterface {
  default void method1(){
    commonCode();
    System.out.println("In method 1");
  }

  default void method2(){
    commonCode();
    System.out.println("In method 2");
  }

  private void commonCode(){
    System.out.println("Executing common code...");
  }
}

Now any class implementing this interface can’t access commonCode() method as it is private.

Private Methods in Java Interface

Rules for private methods in Java interfaces

  1. A private method in an interface has to have a method body, it can’t be just declared as normal public abstract method we usually have in interfaces. Declaring a private method as follows gives compile time error “This method requires a body instead of a semicolon”.
    private void commonCode();
    
  2. As clear from the above point both private and abstract modifier can’t be used together.
  3. If you want to access a private method from a static method in an interface then that method has to be a private static method as you can’t make static reference to the non-static method. For example if you have following code in your interface then you will get compile time error “Cannot make a static reference to the non-static method commonCode() from the type MyInterface”.
    static void method3() {
      commonCode();
    }
    
    private void commonCode(){
      System.out.println("Executing common code...");
    }
    

    The private method should be written as following so that it can be accessed from the static context.

    private static void commonCode(){
      System.out.println("Executing common code...");
    }
    
  4. A private static method can be used from a non-static context too which means a private static method can be invoked from a default method in an interface.

Evolution of interfaces in Java

Here is a summary of the capabilities added to the interfaces in Java and what all features are available to you in Java interfaces now.

private method Java interface
That's all for the topic Private Methods in Java Interface. If something is missing or you have something to share about the topic please write a comment.
You may also like

No comments:

Post a Comment