Java Private Interface Methods: Enhancing Encapsulation and Maintainability
Learn about Private Interface Methods in Java, introduced in Java 9 to allow better encapsulation. These private methods cannot be accessed by implementing classes or sub-interfaces, reducing code duplication and increasing maintainability while keeping method implementations within the interface itself.
Java - Private Interface Methods
Private and static private interface methods were introduced in Java 9. As private methods, these cannot be accessed by implementing classes or sub-interfaces. They were introduced to allow encapsulation, keeping the implementation of certain methods within the interface itself. This feature helps reduce duplication, increases maintainability, and promotes clean code.
Interface Prior to Java 8
Before Java 8, an interface could only have abstract methods and constant variables, requiring implementing classes to implement these methods. Below is an example:
Example
package com.tutorialsarena;
interface Util {
public int sum(int a, int b);
}
public class Tester implements Util {
public static void main(String[] args) {
Tester tester = new Tester();
System.out.println(tester.sum(2, 3));
}
@Override
public int sum(int a, int b) {
return a + b;
}
}
Output
5
In the example above, the implementing class must implement the method as it implements the interface.
Default Method in Interface from Java 8
With the introduction of default methods in Java 8, developers can provide a default implementation of a method within the interface. The implementing class is not required to implement this method. This feature facilitates lambda expressions and allows existing collection frameworks to work with new functional interfaces without requiring all methods to be implemented, thus avoiding code duplication. See the example below:
Example
package com.tutorialsarena;
interface Util {
public default int sum(int a, int b) {
return a + b;
}
}
public class Tester implements Util {
public static void main(String[] args) {
Tester tester = new Tester();
System.out.println(tester.sum(2, 3));
}
}
Output
5
This program produces the same output as the previous example.
Private Method in Interface from Java 9
From Java 9 onwards, interfaces were further enhanced to include private methods. You can now define private as well as private static methods within an interface, which helps encapsulate functionality while maintaining method integrity. Since private methods cannot be inherited, they can only be used within public methods of the interface. Below is an example:
Example
package com.tutorialsarena;
interface Util {
public default int operate(int a, int b) {
return sum(a, b);
}
private int sum(int a, int b) {
return a + b;
}
}
public class Tester implements Util {
public static void main(String[] args) {
Tester tester = new Tester();
System.out.println(tester.operate(2, 3));
}
}
Output
5
Private Static Method in Interface from Java 9
Similarly, you can define private static methods that can be called from both static and non-static methods. Here is an example:
Example
package com.tutorialsarena;
interface Util {
public default int operate(int a, int b) {
return sum(a, b);
}
private static int sum(int a, int b) {
return a + b;
}
}
public class Tester implements Util {
public static void main(String[] args) {
Tester tester = new Tester();
System.out.println(tester.operate(2, 3));
}
}
Output
5
Note that a private static method cannot call a non-static method in an interface and is not accessible outside the interface. Even the implementing class cannot access the private static method.