Java 9 – Private methods in interface

In this topic, we will discuss the private and private static methods inside an interface.

interface in Java is a concept which is used to achieve abstraction. In early versions of Java, an interface can contain only abstract methods and constants until Java 8 which allows to add default and static methods as well.

In Java 9, interface allows creating private and private static methods. It means now an interface can have the following:

  1. Abstract methods
  2. Constant variables
  3. Default methods (added in Java 8)
  4. Static methods (added in Java 8)
  5. Private methods (added in Java 9)
  6. Private static methods (added in Java 9)

Now, let’s understand what is private method and why it is important.

Interface Private Method

A private method declared inside an interface is similar to declared inside a class. It is declared using private access modifier to keep it limited to the interface. These methods can’t be accessible outside the interface and don’t inherit to the interface or implementing class.

The primary purpose behind to add private method was to share common code between non-abstract methods within the interface. Let’s see an example.

Output:

Let’s have a Coffee

Interface Private Static Methods

Like private instance methods, private methods can be static as well. A private static method can not be accessible from outside the interface and can only be accessible inside the default or static methods.

Output:

Let’s have a Coffee
Let’s have a Tea

If we want to share code between instance methods, private instance methods and private static methods both can be used. But If we want to share code between static methods, use private static methods only. See, what happens, if we try to access a private method inside a static method.

Output:

Error : Cannot make a static reference to the non-static method

Key-points to remember

  1. Interface private methods must have implementation body; they can’t be declared as abstract methods.
  2. A static method can call only private static method, but a default method can call both the static and non-static(instance) private methods.
  3. The interface private methods support sharing common code between non-abstract methods(default, static and private).

Conclusion

This topic is all about the private methods in the interface. This feature makes the interface more powerful and advance. Now, interface supports both private and private static methods within an interface.

That’s all about Java 9 Private methods in interface.

Was this post helpful?

Related Posts

  • 12 October

    Java 9 JShell tutorial

    Table of ContentsHello World Example Using JShellWhat can we do with JShell?How to start and stop JShell?Creating variable in JShellJShell as a CalculatorCreating Methods in JShellCreating Class in JShellExecuting Algebraic Expressions in JShellExecuting Conditional Statements in JShellJShell CommandsList the VariablesList the MethodsImport Java PackagesCheck history of commandsEdit code in JShell Edit PadLoad external Java code […]

  • 11 October

    Java 9: Stream API Improvements

    Table of ContentsThe takeWhile() MethodExampleThe dropWhile() MethodExampleThe ofNullable() MethodExampleThe iterate() MethodExampleWas this post helpful? In Java 9, following methods are added to Stream interface of stream package. These methods are default and static. The takeWhile() Method This method is included in Stream interface and used to collect the elements into a stream. It takes all […]

  • 24 September

    Java 9 – Process API Improvements

    Table of ContentsThe Process ClassStart a new Process?MethodsExample to get Process IdExample to get Process InformationProcessHandle InterfaceMethodsExampleProcessHandle.Info Interface MethodsWas this post helpful? In this post, we will see about Java 9 process API improvements. Java improved its Process API in Java 9 version that includes new methods for Process class and two new interfaces ProcessHandle […]

  • 19 September

    Java try with resources

    Table of ContentsOlder approach to close the resourcesJava 7 try with resourcesSyntaxExampleJava 9 Try with resources ImprovementsFinally block with try with resourcesCreate Custom AutoCloseable CodeWas this post helpful? In this post, we will see about Java try with resources Statement. Java try with resources is a feature of Java which was added into Java 7. […]

  • 19 September

    Java 9 – Javadoc Improvement

    Table of ContentsWhat is JavadocSimplified Doclet APISupport for HTML5Improved SearchSupport for Module SystemHow to create HTML Javadoc from Java source file.Run Javadoc CommandWas this post helpful? Java has upgraded its Javadoc for better user interaction and user-experience. It includes a simplified search, HTML generator, Doclet API, etc. What is Javadoc The Javadoc is a tool […]

  • 19 September

    Java 9 – @SafeVarargs Annotation

    Table of [email protected] [email protected] with Final [email protected] with Static MethodJava 9 @SafeVarargs ImprovementWithout @SafeVarargs annotationWas this post helpful? In this post, we will see about @SafeVarargs Annotation in Java 9. The @SafeVarargs is an annotation that is used to perform safe operations. When a method takes variable arguments, then it may cause to unsafe operation, […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.