Private Methods in Java 9 Interfaces

By | June 27, 2021

Java 9 Private Interface Methods:

  • From Java 9 onwards, we can create private non-static/static methods inside an interface.
  • The interface allows us to define private methods that help to share common code between non-abstract/default methods.
  • Prior to Java 9, When you create private methods inside an interface then you will get a compile-time error.

Private methods in interfaces have four rules :

  • Private methods in the interface cannot be abstract.
  • The private method can be used only inside the interface.
  • We can use the private static method inside other static and non-static/ interface methods.
  • private non-static methods cannot be used inside private or public static methods.

Let’s try to understand the above points using the below example.

If you run as Java Application then it will give the below output:  

That’s all about Java 9 private Interface Methods

You May Also Like:

Java 9 Jshell Tutorial
Java 9 Try with Resource Enhancement
Java 9 @SafeVarargs Annotation
Java 9 Collection Factory Methods
Java 9 Stream API improvements

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *