I stumbled upon a small library which has some classes in four packages and one basic class that should be used by who ever will use this library.
I had to review this library and suggested, that the access to some classes and methods should be restricted as the user should only see methods/classes that he really should use. In this case it should be one class with a few methods that can be called.
While my suggestion just popped into my mind, I couldn’t get a real solution on how to do that.
The first ugly idea was: Put the public class in package a and everything else in package a.b which isn’t a good idea.
The package structure prevent the protected modifier as classes in package a.b need to call methods from a class in package a.c. So is there a pattern that can be used to prevent that someone is instantiating a class inside a.b and call their public methods?
If the description of the problem wasn’t good enough, feel free to ask in the comments. Thanks.
Put classes that you don’t want other people to see with default modifier ( basically with no modifier at all ). This will make them package-private.
All classes inside the package will be able to see and use them, but nobody outside the library would be able to instantiate and use them without reflection cheats.
This implies that library should use a single package.