Learn How to Declare Private and Protected Methods in Java 8 Interfaces
When Java 8 was introduced, we could use default methods in interfaces. The main driver for this feature was to allow expansion of an interface while retaining backward compatibility for older interface versions. One example is the introduction of the
stream()
method in the existing Collection
classes.Sometimes, when we want to introduce several default methods, they may share some common code base and then it would be nice if we could use private methods in the interface. This way, we can reuse our code and also prevent it from being exposed to classes that are using or are implementing the interface.
But there is a problem. Private
A Simple Solution
Suppose that we have an interface
Foo
with two methods; bar()
and bazz()
that both are to return some hard-to-calculate result emanating form some shared code like this:public interface Foo {By introducing a
default int bar() {
return complicatedMethodWithManyLinesOfCode();
}
default int bazz() {
return complicatedMethodWithManyLinesOfCode() + 1;
}
// Will not work in Java 8 because interface methods cannot be private!
private int complicatedMethodWithManyLinesOfCode() {
// Actual code not shown...
return 0;
}
}
class
that holds the private method, we can "hide" the method from outside access and almost get away with private methods in Java 8 interface. It can be done like this: public interface Foo {The method
default int bar() {
return Hidden.complicatedMethodWithManyLinesOfCode();
}
default int bazz() {
return Hidden.complicatedMethodWithManyLinesOfCode() + 1;
}
class Hidden {
private static int complicatedMethodWithManyLinesOfCode() {
// Actual code not shown...
return 0;
}
}
}
Foo:complicatedMethodWithManyLinesOfCode
is not visible from outside classes or interfaces but the Hidden
class itself can be seen. However, methods and fields in Hidden
cannot be seen if they are private.This scheme can also be applied for protected interface method access. Technically, we could extend the
Hidden
class in an interface that also extends the original interface Foo
. Remember that protected methods are also package visible, so if we extend or use the interface from the same package, the protected methods are visible (as they always are).One drawback is that the hidden methods cannot access other methods in the interface. This latter drawback can easily be fixed by letting the hidden static method take a parameter of the interface type. Suppose that the
complicatedMethodWithManyLinesOfCode
method needs another value from the Foo
interface that can be obtained via some interface method named buzz()
, then it could look something like this: public interface Foo {
default int bar() {
return Hidden.complicatedMethodWithManyLinesOfCode(this);
}
default int bazz() {
return Hidden.complicatedMethodWithManyLinesOfCode(this) + 1;
}
int buzz();
class Hidden {
private static int complicatedMethodWithManyLinesOfCode(Foo foo) {
// Actual code not shown...
return 0 + foo.buzz();
}
}
}