In Java 9, Private Methods in Interfaces has been introduced. This enables code sharing between non-abstract methods in an interface. In this article apart from understanding the Private Method in Interface, we will learn how the Interface has evolved in different versions of Java.
Java 7 (Or) Earlier Interface:
In Java 7 or earlier versions of Java, Interface is very simple they can have only abstract methods and constants. Since the methods are abstract it cannot have implementations. If we want to have both abstract and non-abstract methods then there is no other go we need to for abstract class only.
public interface Calculator { public void add(int a, int b); public void subtract(int a, int b); public void multiply(int a, int b); public void divide(int a, int b); }
Here in our Calculator interface we have four unimplemented abstract methods add (), subtract (), multiply (), divide (). The class which implements the Calculator interface will provide the body for the abstract methods (add (), subtract (), multiply (), divide ()).
Java 8 Interface:
Later in Java 8, Default Methods and Static methods has been added to the interface, which helped us to have an implementation for a method in an interface all we need to do is just add “default” keyword in front of the method. In Java 8 along with abstract methods and constants, it has default method and static method.
Now our Calculator interface can be re-written like below (with body)
package com.javainterviewpoint; import java.util.Scanner; public interface Calculator { public default void add() { Scanner scanner = new Scanner(System.in); System.out.println("Enter Number1 : "); int a = scanner.nextInt(); System.out.println("Enter Number2 : "); int b = scanner.nextInt(); System.out.println(a + b); } public default void subtract() { Scanner scanner = new Scanner(System.in); System.out.println("Enter Number1 : "); int a = scanner.nextInt(); System.out.println("Enter Number2 : "); int b = scanner.nextInt(); System.out.println(a - b); } public default void multiply() { Scanner scanner = new Scanner(System.in); System.out.println("Enter Number1 : "); int a = scanner.nextInt(); System.out.println("Enter Number2 : "); int b = scanner.nextInt(); System.out.println(a * b); } public default void divide() { Scanner scanner = new Scanner(System.in); System.out.println("Enter Number1 : "); int a = scanner.nextInt(); System.out.println("Enter Number2 : "); int b = scanner.nextInt(); System.out.println(a / b); } }
We have provided the implementations for all the methods in the above code, but when we look into the code we can understand that we have a small issue here. We have some redundant code such as Creating a Scanner object and Reading both the numbers which can be moved to a common method but as an API developer we never want to expose the logic to the consumer. There is no solution for this in Java 8, which is the main reason for the introduction of Private Methods in Java 9.
Java 9 Interface:
In Java 9, Private Methods has been introduced which lets us share the code between the other public methods. Now we can re-write the code like below
package com.javainterviewpoint; import java.util.Scanner; public interface Calculator { public default void add() { calc("add"); } public default void subtract() { calc("subtract"); } public default void multiply() { calc("multiply"); } public default void divide() { calc("divide"); } private default void calc(String operation) { Scanner scanner = new Scanner(System.in); System.out.println("Enter Number1 : "); int a = scanner.nextInt(); System.out.println("Enter Number2 : "); int b = scanner.nextInt(); if (operation.equals("add")) { System.out.println(a+b); } else if (operation.equals("subtract")) { System.out.println(a-b); } else if (operation.equals("multiply")) { System.out.println(a*b); } else { System.out.println(a/b); } } }
Now we have moved the redundant code a single private method that is not visible to the consumer as well. The Private Method must have a body (must be implemented) and have you cannot have abstract specifier for the Private Method.
Interfaces in Java 9 will now have abstract methods, constants, default methods, static methods, private methods and private static methods. Happy Learning 🙂
Vinayak A says
Hello Sir,
Thanks for sharing such a nice post.
Pumpkin says
Really helpful article..!!