Can anyone generalize the visibility options for Java interfaces?

I have two questions:

1) When will you use package-private interface?

2) Is there a way to have a public interface that is private to implementation outside of its package?

+1


a source to share


3 answers


1) when the interface is only needed in the scope of your package. For example, an interface can make your code more readable, but the packages that call it don't need it.



2) No, this is impossible. For more information see the link. Interface does not define implementation. How could you close it for implementation?

+5


a source


Batch private interfaces are only useful in cases where the pattern strategy is implemented, where there are multiple implementations that you can use but don't want the world to know about types.



+2


a source


1) You may have utility methods used by multiple classes in your package that should never be called externally. Or, only one class may need them, but the class is so large that you want to move some of the methods to another class for ease of maintenance.

2) I would have to try (sorry, I'm heading for the door now), but you could effectively do this by declaring a protected default constructor in the interface.

0


a source







All Articles