What is equivalent to a package-private interface for encapsulating internal functionality of a package?

StackOverflow https://stackoverflow.com/questions/20507008

Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top