2

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.

Rylander
  • 19,449
  • 25
  • 93
  • 144
  • 2
    You could swap the interfaces for abstract classes. – Bart Dec 10 '13 at 22:53
  • 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 Dec 10 '13 at 22:58
  • see http://stackoverflow.com/questions/423032/interfaces-in-java-cannot-make-implemented-methods-protected-or-private – CupawnTae Dec 10 '13 at 23:03
  • @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. – Rylander Dec 10 '13 at 23:04
  • @Bart are there any downsides to using an abstract class for this? Could you make that an answer. – Rylander Dec 10 '13 at 23:19
  • @CupawnTae Thanks for that link, however the primary answers to that question deal with the ability to access the methods. Where as I am also concerned about how various development tools will interpret and report these methods. – Rylander Dec 10 '13 at 23:37
  • Stuhlo gave a pretty good answer :) – Bart Dec 11 '13 at 05:13

1 Answers1

1

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.

stuhlo
  • 1,479
  • 9
  • 17