Java 9

Java 9 Private Interface Methods with code sample

Posted On
Posted By admin

In this blog post, I will be explaining Java 9 private interface methods. This is another of Java 9’s new features.

Introduction

Java 8 introduced default and static methods in interfaces. Default methods are nothing but methods with some method bodies. Such methods have the keyword “Default” specified. Private interface methods allow code reuse in default methods.  So if there is some common code across two or more default methods, this code can be placed in a private method

Code Sample

public interface MyInterface {
  
  default void method1() {
    commonCode();
    System.out.println("Code for method1");
  }
  
  
  default void method2() {
    commonCode();
    System.out.println("Code for method2");	
  }
  
  private void commonCode() {
    System.out.println("Common code");
  }

}

In the above example, the interface MyInterfacehas 2 default methods, method1and method2.In addition, there is a method commonCodewhich is a private method. This is invoked from both method1and method2. So any code that is common can be placed here.

Conclusion

So, in this article, we learned about private interface methods introduced by Java 9.

If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment