Quick revision
The short answer
Hiding static factory methods can never give polymorphic behaviour, because a static call is bound at compile time to the type named in the source, so the design misuses the language and teams will keep getting the wrong implementation. We guide the redesign towards instance-based strategies or explicit factory interfaces injected where needed, keep the old static methods as deprecated delegates with a removal plan, add a static analysis rule that forbids hiding static methods, and migrate the most used call sites first while measuring how many uses remain.
What to remember
In this answer, we saw that static factory hiding cannot provide polymorphism because static calls are bound at compile time to the type named in the source, so inherited helpers always call the base version. We replaced it with instance-based strategies and factories injected where needed, kept the old statics as deprecated delegates with forRemoval and usage counters, forbade new static hiding with a build rule, and migrated the most used call sites first while tracking remaining uses towards removal.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Hiding static factory methods can never give polymorphic behaviour, because a static call is bound at compile time to the type named in the source, so the design misuses the language and teams will keep getting the wrong implementation. We guide the redesign towards instance-based strategies or explicit factory interfaces injected where needed, keep the old static methods as deprecated delegates with a removal plan, add a static analysis rule that forbids hiding static methods, and migrate the most used call sites first while measuring how many uses remain.
Why this question is asked
Legacy frameworks often grew before dependency injection was common, and static factories were the easy way to let teams customise behaviour. The trouble is that the customisation works only when every caller writes exactly the right class name, so bugs depend on how a line was typed. Interviewers ask this at senior level because the fix is partly technical, choosing a design that the language supports, and partly a migration across many teams who cannot all stop and rewrite. In this answer, we will see why the current design fails, choose a replacement, plan a compatible migration path, add guard rails, and decide how to order and measure the work.
1Why the Current Design Keeps Failing
A static method belongs to a class, not to an object. When a subclass declares a static method with the same signature as its parent, it hides the parent's method rather than overriding it, and the compiler decides which one to call from the type written in the source code. No run-time lookup happens. Here is a reduced version of what such a framework typically looks like.
The second line is the bug teams keep hitting. exportAll is an inherited static method, and the call to create() inside it was compiled inside Exporter, so it is permanently bound to Exporter.create(). Writing PdfExporter in front of exportAll changes nothing. The same happens wherever framework code calls a factory by the base class name. So the problem is not careless teams; it is a design that asks the language to do something it never does.
2Choosing the Replacement Design
We want the choice of implementation to be made once, explicitly, and then carried by an object. The simplest form is an interface for the behaviour plus a factory interface or plain constructor injection, so the implementation is chosen where the application is wired, not at every call site.
For code outside Spring, an explicit factory class or a Supplier<ReportWriter> passed in does the same job. The important change is that variation now comes from an instance, where dynamic dispatch works, and there is no static method left to hide. Each option should be judged by how well it fits the teams' current wiring; we do not need a single answer for every service.
3A Compatible Migration Path
Many teams depend on the old static calls, so we cannot delete them in one release. We keep them working as thin delegates over the new design and mark them deprecated with a clear removal plan.
- Ship the new interfaces and ExportServiceOld static methods unchanged
- Turn old statics into deprecated delegates
forRemovalwarnings, usage counters - Forbid new static hiding in the buildArchitecture or lint rule
- Migrate the most used call sites firstRanked by usage counts
- Remaining uses near zero?YesRemove in the next major versionNoHelp the remaining teams, then check again
5 steps in order. Play them, or step through them one at a time.
forRemoval = true makes javac print a removal warning at every call site, which is stronger than an ordinary deprecation warning. The delegate also records its use, so we know how many callers remain in production, not only in source code. And we publish a short migration guide with before and after snippets, because a deprecation without a clear replacement mostly creates annoyance.
4Guard Rails: Forbidding Static Hiding
While the migration runs, we must stop new code from adding more of the old pattern. Hiding static methods is easy to detect mechanically, so a build rule is cheaper than relying on review. A small custom ArchUnit condition is one way to express it.
We can add a second rule that flags static methods called through a subclass name, which is how people try to select a hidden method. Existing violations can be frozen as a baseline so the build fails only on new ones.
5Ordering the Work and Measuring Progress
Not all call sites are equal. Usually a few services or shared modules account for most calls, so we rank them by the usage counters and source search, and migrate the top ones first, often pairing with the owning team or offering an automated refactoring such as an OpenRewrite recipe. A typical plan might take the top ten call sites, about 70 percent of uses, in the first month.
We then publish a simple chart of remaining uses by team each week. It turns the migration into a visible, shrinking number rather than an open-ended request, and it tells us when removal in the next major version is safe.
6Follow-up questions
Why not fix the framework by making exportAll look up the factory with reflection?
That would make the hidden methods appear to work, for example by inspecting the caller's class, but it hides the real problem behind magic that is slow, fragile and hard to debug. It would also encourage more code to rely on static hiding. The better fix is a design where the language's normal dynamic dispatch does the work.
How do you handle teams that cannot migrate before the removal date?
We find out early from the usage data, talk to them about their constraints, and help directly where it is blocking, for example by pairing or by providing a recipe. If a team truly cannot move, we can extend the date for them by keeping the deprecated delegates in a separate compatibility module, so the main library still drops them.
Should every static factory be removed?
No. Static factories such as List.of or a value object's parse method are fine, because they do not try to vary behaviour by subclass. The problem is only static methods used as extension points through hiding.
7Conclusion
In this answer, we saw that static factory hiding cannot provide polymorphism because static calls are bound at compile time to the type named in the source, so inherited helpers always call the base version. We replaced it with instance-based strategies and factories injected where needed, kept the old statics as deprecated delegates with forRemoval and usage counters, forbade new static hiding with a build rule, and migrated the most used call sites first while tracking remaining uses towards removal.
Open with
I'd start by explaining to the teams that the framework is asking Java to do something it never does. Static methods are bound at compile time to the type named in the source, so a subclass's static method hides the parent's rather than overriding it.
Then, if they want more
That's why it works when someone writes PdfExporter.create, but an inherited static helper like exportAll, which calls create internally, always gets the base version. It's a design problem, not a discipline problem.
For the redesign, I'd move variation onto instances: an interface for the behaviour and either constructor injection or an explicit factory, so the implementation is chosen once where the application is wired and normal dynamic dispatch does the rest.
Because many teams depend on the old calls, I'd keep the static methods as thin delegates to the new design, mark them Deprecated with forRemoval true and a target version, add a usage counter, and publish a migration guide with before and after examples.
In parallel, I'd add a build rule, for example a custom ArchUnit check, that forbids new static methods hiding superclass statics, with existing violations frozen as a baseline.
Then I'd migrate by impact: rank call sites by usage, take the top ones first with the owning teams or an OpenRewrite recipe, and publish remaining uses weekly until it's safe to remove them in the next major version.
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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "A legacy utility framework in your company relies on subclasses hiding static factory...": https://scrivio-interview-library.netlify.app/java/oop-principles/1adba4b34a/