2
votes

I am attempting to encapsulate all of the internal functionality of a service package. Most of my classes and methods are package-private. I have some internal interfaces that I don't want to expose outside of the package. I can make the interface itself package-private however all of the methods are still public (the default scope for interface methods).

What are my options for eliminating the public method signatures from my internal implementations in this package?

I am using interfaces so that I can easily switch out implementations using spring.

Some Things to Consider: Development tools that use source code analysis will report the interface methods as public API methods. For example a UML generator would generate a misleading UML diagram that incorrectly shows this as a public method.

1
You could swap the interfaces for abstract classes. - Bart
Why is it necessary to reduce the visibility of the interface members? If the interfaces themselves are package-private, what does it matter if its members are public? Is the issue that you have public classes implementing these interfaces, and you don't want the interface method implementations to be publicly visible? - Mike Strobel
@MikeStrobel A use case could be a public class that implements methods that I don't want exposed. Other things to consider are various development tools that use source code inspection and report "public" methods, for example a tool that list public APIs. - Mike Rylander
@Bart are there any downsides to using an abstract class for this? Could you make that an answer. - Mike Rylander

1 Answers

1
votes

One possible solution, as already @Bart has pointed out, is to use abstract classes instead of interfaces. A possible problem connected with this concept is a single-inheritance issue.

Another solution could be a separation of the "private" interfaces in a different package which doesn't need to be published together with your service package thought this method can quite ruin semantics of the interfaces, specially if one logical interface would have "private" and "public" part.

Last work around which has come to mind is to utilize some patter e.g. Double dispatch or Visitor could be useful.