[JLBP-2]

Minimize API surface

Avoid exposing types from your dependencies.

Use package-protected classes and methods for internal APIs that should not be used by consumers.

Do not mark methods and classes public by default. Assume non-public until a need is known.1

Design for inheritance or prohibit it. That is, mark classes final unless there is a clear reason for them to be subclassed. Mark methods in non-final classes final unless they are meant to be overridden.2

Prefer fewer packages over more packages to avoid unnecessarily publicizing internal details, since any dependency across package boundaries needs to be public. (We may revisit this when we can rely on the new module system in Java 11 or later.)

If you absolutely must create public classes that clients should not depend on, one of the superpackages that contains these classes should be named internal. For example, com.foo.utilities.internal.xml.

1 Bloch, Joshua. “Item 15: Minimize the accessibility of classes and members.” Effective Java, 3rd Edition. Boston: Addison-Wesley, 2018. p. 73

2 Bloch, Joshua. “Item 19: Design and document for inheritance or else prohibit it.” Effective Java, 3rd Edition. Boston: Addison-Wesley, 2018. p. 93