As we know that Java 8 allowed us to create default and static methods in Interface. The intention was to have new methods added to the interfaces without breaking the classes that already implemented those interfaces. Java 9 has introduced another new feature, Java 9 SE onwards we can have private methods in interfaces. In this guide, we will learn why they added this feature, what is the use of it and how to work with it.
Why Java 9 allows us to have private methods in Interfaces?
Java 9 introduced private methods in interfaces to remove the redundancy by sharing the common code of multiple default methods through private methods.
To understand this we have to take an example in Java 8 (without private methods) and then we will take the same example with Java 9(using private methods).
Example in Java 8 – Multiple default methods with duplicate code (common code)
In this example we will see how default methods can have duplicate code which unnecessary increase the lines of code and makes the code less-readable. We will take the same example again using private methods to see how can private methods help us to avoid duplicate code.
interface MyInterfaceInJava8 { default void method1() { System.out.println("Starting method"); System.out.println("Doing someting"); System.out.println("This is method1"); } default void method2() { System.out.println("Starting method"); System.out.println("Doing someting"); System.out.println("This is method2"); } } public class JavaExample implements MyInterfaceInJava8{ public static void main(String args[]) { JavaExample je = new JavaExample(); je.method1(); je.method2(); } }
Output:
Example in Java 9 – Default methods sharing common code using private methods
We are taking the same example that we have seen above. This time we will introduce a private method to share the common code.
interface MyInterfaceInJava9 { default void method1() { //calling private method printLines(); System.out.println("This is method1"); } default void method2() { //calling private method printLines(); System.out.println("This is method2"); } private void printLines() { System.out.println("Starting method"); System.out.println("Doing someting"); } } public class JavaExample implements MyInterfaceInJava9{ public static void main(String args[]) { JavaExample je = new JavaExample(); je.method1(); je.method2(); } }
Output:
Starting method Doing someting This is method1 Starting method Doing someting This is method2
As you can see the output is same and the code size has been reduced.
Based on this we can say that the advantages of having private methods in interfaces are:
1. Allows default methods to share common code to avoid duplicate code (redundancy)
2. Improves readability of code.
Java 9 – Private Static methods
Till now we have leaned how to use private methods in interfaces to share common code of default methods. Java 9 also allows us to have private static methods in interfaces.
Since java 8 we can have static methods in interfaces along with default methods. We can not share the common code of static methods using the non-static private method, we must have to use the private static method to do that.
Lets take an example to understand this.
interface MyInterfaceInJava9 { static void method1() { //calling private method printLines(); System.out.println("This is method1"); } static void method2() { //calling private method printLines(); System.out.println("This is method2"); } //this must be static else we will get compilation error private static void printLines() { System.out.println("Starting method"); System.out.println("Doing someting"); } default void mymethods() { method1(); method2(); } } public class JavaExample implements MyInterfaceInJava9{ public static void main(String args[]) { JavaExample je = new JavaExample(); je.mymethods(); } }
Output:
Starting method Doing someting This is method1 Starting method Doing someting This is method2
Here is the screenshot of Eclipse Oxygen.
rakesh sharma says
hello, everyone I have a doubt in java 9 features in interface is that if private static method can also used in default method then what is the purpose of private instance method