Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

Your company's shared libraries expose many public classes in packages named impl or internal, and consumers depend on them, blocking refactoring. How do you fix this over time?

Package-private only works inside one package, so a library spread over several packages has to make its internals public, and consumers start depending on them. We fix it over time by marking what the supported API is, measuring who uses the internals, offering supported replacements with a deprecation and migration path, and then closing the internals for real: with JPMS exports where the library can be a module, and otherwise with naming conventions, ArchUnit rules and API compatibility checks in the build, all under semantic versioning so breaking changes are explicit.

Why this question is asked

Every company with shared libraries reaches this point. A team wants to refactor a library, finds that half the organisation imports classes from com.acme.billing.internal, and gives up because any change breaks someone. Interviewers ask this design question because the answer has both a technical side, what Java can and cannot enforce, and an organisational side, how to move many consumers without a big-bang break. In this answer, we will see why the internals became public in the first place, decide what the supported API is, look at the enforcement options with and without modules, and lay out a migration plan in phases.

1Why the Internals Became Public

Java's only built-in way to hide a class from other code, short of modules, is package-private visibility: no modifier, visible only inside the same package. That works while a library fits in one package. As soon as it grows into com.acme.billing, com.acme.billing.tax and com.acme.billing.internal, the classes in different packages need to call each other, and the only modifier that crosses package boundaries is public. Java has no notion of visible to my own library but not to others, so the internal classes become public.

The package name internal is then just a hint. The compiler lets any consumer import those classes, IDE auto-import happily suggests them, and once one team uses a class because it solved a problem quickly, the library has an accidental API it never designed.

2Decide and Mark the Supported API

We cannot protect something we have not defined. The library team first writes down which packages and types are supported, and marks them in the code so tools can read it. A common choice is an annotation such as API Guardian's @API(status = STABLE) or @API(status = INTERNAL), which JUnit uses for the same purpose; a simple naming rule, where everything under an internal or impl package is unsupported, works too.

Java
@API(status = API.Status.STABLE, since = "4.0")
public interface InvoiceService { Invoice issue(Order order); }
@API(status = API.Status.INTERNAL)
public final class TaxTableLoader { ... } // public for technical reasons, not supported

Making the line visible matters even before enforcement, because it changes the conversation from you broke us to you used an internal class that was marked as such.

3Measure Who Depends on the Internals

Before closing anything, we find out what consumers actually use. Static analysis across the organisation's repositories, for example jdeps on the consumers' jars, or a code search for imports of the internal packages, gives a list of internal classes used and by which services. Usually the list is short: a few classes provide something the public API is missing, such as a parser or a test helper.

That list drives the plan. Each heavily used internal class becomes either a new supported API, a documented replacement in the existing API, or an explicit no. We publish the numbers, such as 42 services import 11 internal classes, and track them down to zero.

4Deprecate With a Migration Path

For each internal class in use, the next minor release adds the supported replacement and marks the internal use as deprecated, with a message that names the replacement and the release in which access will close. Consumers see the warning in their builds and have at least one release cycle, for example two quarters, to move.

Java
/**
* @deprecated internal; use {@link InvoiceService#issue(Order)} instead.
* Access from outside com.acme.billing closes in 5.0.
*/
@Deprecated(since = "4.3", forRemoval = true)
public final class InvoiceBuilderImpl { ... }

Where possible, we offer an automated migration too, such as an OpenRewrite recipe that rewrites the imports, because consumers move much faster when the change is one command.

5Enforce the Boundary

When the library can become a JPMS module, enforcement is built in. Its module-info.java exports only the API packages; public classes in non-exported packages are then inaccessible to code on the module path, and even reflection fails unless the package is opened. The catch is that most Spring Boot applications run on the classpath, where module boundaries are not enforced, so JPMS protects module-path consumers only.

Without modules, we enforce in builds. The library's own CI runs an API compatibility checker, such as japicmp or Revapi, against the last release, so any incompatible change to the supported API fails the build unless the major version changes. Consumers' builds get a shared ArchUnit rule, distributed through a common test library or the company's parent build, that fails when code imports com.acme..internal.. packages.

Java
// shared ArchUnit rule run in every consumer's test suite
@ArchTest
static final ArchRule no_internal_imports =
noClasses().that().resideOutsideOfPackage("com.acme..")
.should().dependOnClassesThat().resideInAPackage("com.acme..internal..");
Step throughClosing internal packages over time
  1. Mark the supported APIannotations or package naming
  2. Measure internal usejdeps, code search: who imports what
  3. Is the internal class widely used?
    YesAdd a supported replacementNoDeprecate it directly
  4. Deprecate with a migration pathmessage, replacement, closing release
  5. Enforceexports for modules; ArchUnit and japicmp for the rest
  6. Close in the next major versioninternals now free to change

6 steps in order. Play them, or step through them one at a time.

Finally, semantic versioning makes the contract visible: breaking changes to the supported API only in major versions, and changes to internals allowed in any release, which is now safe because nobody can depend on them without the build failing.

6Follow-up questions

Why not just make the library a single package so package-private works?

For a small library that can be right, but for a large one it trades one problem for another: a single package with hundreds of classes is hard to navigate, and package-private then hides nothing from the library's own unrelated parts. Splitting into Maven or Gradle modules, an API module and an implementation module, is often the better structural fix, since consumers depend only on the API artifact.

What do you do about a team that refuses to migrate?

We make the cost of staying visible and the cost of moving small. They get the deprecation warnings, a recipe that does the migration, and a date. If they truly need something the API lacks, that is a signal to extend the API. After the date, they can stay on the old major version, but they no longer get fixes, which is their decision to make with that information.

Does an API compatibility checker replace review?

No, it complements it. Tools catch binary and source incompatibilities, such as a removed method or a changed signature. They cannot judge behavioural changes, such as a method that now throws in a new case, so changes to the supported API still need a reviewer who owns the contract.

7Conclusion

In this answer, we saw that internals of multi-package libraries become public because package-private cannot cross package boundaries, and that consumers then depend on them. We fixed it in phases: mark the supported API, measure internal use, add replacements and deprecate with a migration path, and enforce the boundary with JPMS exports where modules are used, or with ArchUnit rules and API compatibility checks otherwise, under semantic versioning so that breaking changes are explicit and internals are free to change.

What to say in the interviewAbout 80 seconds aloud

Open with

The root cause is that package-private only works inside one package. Once a library spans several packages, its own classes need public to talk to each other, so internal and impl packages end up public, and a package name is only a hint.

Then, if they want more

I would fix it in phases rather than with one breaking release. First, define and mark the supported API, with an annotation like API Guardian's status or a clear naming rule, so the line is explicit.

Second, measure: run jdeps or a code search across consumer repositories to see which internal classes are used and by whom; usually it is a short list that shows gaps in the API.

Third, for each one, add a supported replacement, deprecate the internal one for removal with a message naming the replacement and the closing release, and ideally ship an OpenRewrite recipe. Fourth, enforce. Where the library can be a JPMS module, I export only the API packages, knowing that only module-path consumers are protected.

For the classpath majority, a shared ArchUnit rule in consumers' builds forbids internal imports, and japicmp or Revapi in the library's CI blocks incompatible API changes. Then I close access in the next major version under semantic versioning, after which the team can refactor internals freely.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Your company's shared libraries expose many public classes in packages named impl or...": https://scrivio-interview-library.netlify.app/java/classes-and-objects/80a6bcacb1/