java Interface private methods with examples


In this article we will know about Java interface private methods, that are introduced in Java 9 version and also know about Rules for writing private methods in Interface with examples.

1. Allowed modifiers for interface methods until Java 7 :

Until java 7 every interface method is public and abstract by definition, means these modifiers whether we declared or not every interface method is public and abstract. As every interface method is always public and abstract we can’t use the following modifiers for interface methods.
private, protected, final, static, synchronized, native, strictfp.

2. Allowed modifiers for interface methods in Java 8 :

In java 8, in addition to public and abstract modifiers, defaultstatic and strictfp also permitted for interface methods. To know more about Java 8 default and static methods in interface you can read the following articles.

  1. How to write default methods in interface in Java 8?
  2. How to write static methods in interface in Java 8?

3. Allowed modifiers for interface methods in Java 9 :

In Java 9, In addition to the above modifiers, private also permitted to use as modifier for interface methods. Below are all the permitted modifiers for interface methods from Java 9 on wards.

privatepublicabstractdefaultstatic and strictfp.

4. Rules for writing private methods in java 9 :

  1. Private interface method modifier combination with private and abstract not allowed.
  2. private method can be used only inside interface, can’t be inherited or accessible out side of interface.
  3. Generally as static methods can call from both static and instance area, private static methods can be called from inside instance method or static method inside interface.
  4. private non-static method is not allowed to call from static method with in interface. In general instance methods can’t be called from static context with in class or interface.
  5. private strictfp combination also allowed for interface methods in java 9, but strictfp with abstract combination not permitted. strictfp is very rarely used modifier in java. strictfp means strictly floating point, that ensures that you get exactly the same results from your floating point calculations on every platform. This is out of scope of current topic, for more read on strictfp.

5. Why do we use private methods in interfaces ?:

  1. Code re usability. Since java 8 the default and static methods also valid inside interfaces, if any piece of code re usable for multiple methods with in same interface, instead of writing that re usable code out side of interface, we can write private method and that can be re used with in the interface.
  2. Also can achieve the hiding the implementation of methods and then expose only methods that you want show to client.

6. Example for Interface private methods

public interface InterfacePrivateMethodsDemo {

	public List getUserNames();
    static void printEvenItems() {
       getDataStream().filter(i -> i%2==0).forEach(System.out::println);
   static void printLOddItems() {
       getDataStream().filter(i -> i%2!=0).forEach(System.out::println);
   private static Stream<Integer> getDataStream(){
       List<Integer> list = Arrays.asList(10,13,5,15,12,20,11,25,16);
       return; // Creating Stream from collection source
   public static void main(String[] args) {
   //10, 12, 20, 16, 13, 5, 15, 11, 25


In this article we have covered Java interface private methods, that are introduced in Java 9 version and also covered Rules for writing private methods in Interface with an example.


Please enter your comment!
Please enter your name here