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, default
, static
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.
- How to write default methods in interface in Java 8?
- 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.
private
, public
, abstract
, default
, static
and strictfp
.
4. Rules for writing private methods in java 9 :
- Private interface method modifier combination with
private
andabstract
not allowed. private
method can be used only inside interface, can’t be inherited or accessible out side of interface.- Generally as
static
methods can call from both static and instance area,private static
methods can be called from inside instance method orstatic
method inside interface. - private non-static method is not allowed to call from
static
method with in interface. In general instance methods can’t be called fromstatic
context with in class or interface. private strictfp
combination also allowed for interface methods in java 9, butstrictfp
withabstract
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 onstrictfp
.
5. Why do we use private methods in interfaces ?:
- 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.
- 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 list.stream(); // Creating Stream from collection source } public static void main(String[] args) { printEvenItems(); printLOddItems(); } //Output //10, 12, 20, 16, 13, 5, 15, 11, 25 }
Conclusion
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.