Javatpoint Logo
Javatpoint Logo

Java 9 Private Interface Methods

In Java 9, we can create private methods inside an interface. Interface allows us to declare private methods that help to share common code between non-abstract methods.

Before Java 9, creating private methods inside an interface cause a compile time error. The following example is compiled using Java 8 compiler and throws a compile time error.

Java 9 Private Interface Methods Example

Output:

PrivateInterface.java:6: error: modifier private not allowed here

Note: To implement private interface methods, compile source code using Java 9 or higher versions only.

Now, lets execute the following code by using Java 9. See the output, it executes fine.


Java 9 Private Interface Methods Example

Output:

Hello... I'm private method

Such like, we can also create private static methods inside an interface. See, the following example.


Java 9 Private Static Methods Example

Output:

Hello... I'm private method
I'm private static method

Next TopicTry-With Resources




Please Share

facebook twitter google plus pinterest

B.Tech / MCA


Learn Latest Tutorials