Sub topic 14
Private Helper Methods in Interfaces
In the last chapters you added default methods and static methods to interfaces. Once an interface has several default methods, they often repeat the same few lines of code. Java 9 added a fix for this: private interface methods.
A private interface method is a method inside an interface that only that interface can use. Classes that implement the interface cannot see it, and neither can code that calls the interface.
Why You Need Them
Imagine two default methods that build a message in the same way. Without a helper, you copy the same code into both. Copied code is risky: if you change one copy, you must remember to change the other.
You could move the shared code into a new default method. But a default method is public, so every caller could use it. You probably don't want other code calling a small internal step. A private method keeps that step hidden.
A Worked Example
interface Greeter {
String name();
default String hello() {
return wrap("Hello");
}
default String bye() {
return wrap("Goodbye");
}
private String wrap(String word) {
return word + ", " + name() + "!";
}
static String loud(String text) {
return shout(text);
}
private static String shout(String text) {
return text.toUpperCase();
}
}
class FriendlyGreeter implements Greeter {
@Override
public String name() {
return "Ana";
}
}
public class Main {
public static void main(String[] args) {
Greeter g = new FriendlyGreeter();
System.out.println(g.hello()); // prints: Hello, Ana!
System.out.println(g.bye()); // prints: Goodbye, Ana!
System.out.println(Greeter.loud(g.hello())); // prints: HELLO, ANA!
// g.wrap("Hi"); // compile error: wrap is private
}
}The two default methods, hello and bye, share one helper called wrap. The helper can call name(), which is the abstract method the class supplies. The static method loud uses a private static helper called shout.
The Rules
| Kind of private method | Has a body? | Who can call it |
|---|---|---|
| `private` (instance) | Yes, always | Default methods and other private instance methods |
| `private static` | Yes, always | Any method in the interface, static or not |
Some other points to remember:
- A private method must have a body. It cannot be abstract, because no class could ever implement it.
- You cannot mark a private method
default. - A private instance method cannot be called from a static method, because a static method has no object to call it on.
- You need Java 9 or newer.
Exercise
FriendlyGreeter tries to call wrap("Hi") inside its own method. Will this compile? Why or why not?
Show answer
No. The method wrap is private to the Greeter interface. Implementing classes cannot see private interface methods, so the compiler reports an error.
Exercise
Inside Greeter, a new static method calls wrap("Hey"). Will it compile?
Show answer
No. The method wrap is a private instance method, and a static method has no object to call it on. To use the helper from a static method, make the helper private static and give it what it needs, such as the name, as a parameter.