神刀安全网

Java 8: Declare Private and Protected Methods in 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 and protected access in interfaces were postponed to Java 9. So how can we use private interface methods in Java 8 today?

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 {      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;     }  }

By introducing a 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 {      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;         }     }  }

The method 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();         }     }  }

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Java 8: Declare Private and Protected Methods in Interfaces

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮