Scrivio Interview library
LeadershipAdvanced7 min read · updated 12 November 2026

A legacy utility framework in your company relies on subclasses hiding static factory methods of base classes to change behaviour, and teams keep getting the wrong implementation. How would you guide the redesign?

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.

Java
public class Exporter {
public static Exporter create() { return new Exporter(); } // default
public static void exportAll(List<Report> rs) {
Exporter e = create(); // always Exporter.create()
rs.forEach(e::write);
}
void write(Report r) { /* CSV */ }
}
public class PdfExporter extends Exporter {
public static Exporter create() { return new PdfExporter(); } // hides Exporter.create()
@Override void write(Report r) { /* PDF */ }
}
PdfExporter.create().write(report); // PDF: the name PdfExporter picks the hidden method
PdfExporter.exportAll(reports); // CSV: exportAll is inherited, and inside it create() means Exporter.create()

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.

Java
public interface ReportWriter { void write(Report r); }
public final class ExportService {
private final ReportWriter writer; // chosen once, at wiring time
public ExportService(ReportWriter writer) { this.writer = writer; }
public void exportAll(List<Report> rs) { rs.forEach(writer::write); }
}
@Configuration
class ExportConfig {
@Bean ReportWriter reportWriter() { return new PdfReportWriter(); } // one place decides
}

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.

Java
public class Exporter {
/** @deprecated inject a ReportWriter into ExportService instead; removal planned for 6.0 */
@Deprecated(since = "5.2", forRemoval = true)
public static void exportAll(List<Report> rs) {
DeprecationMetrics.record("Exporter.exportAll"); // counts remaining callers
new ExportService(Writers.configuredDefault()).exportAll(rs);
}
}
Step throughMigrating from hidden static factories
  1. Ship the new interfaces and ExportServiceOld static methods unchanged
  2. Turn old statics into deprecated delegatesforRemoval warnings, usage counters
  3. Forbid new static hiding in the buildArchitecture or lint rule
  4. Migrate the most used call sites firstRanked by usage counts
  5. 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.

Java
// A sketch of a custom ArchUnit rule
static final ArchCondition<JavaClass> notHideStaticMethods =
new ArchCondition<>("not hide a static method of a superclass") {
@Override public void check(JavaClass c, ConditionEvents events) {
for (JavaMethod m : c.getMethods()) {
if (!m.getModifiers().contains(JavaModifier.STATIC)) continue;
c.getAllRawSuperclasses().stream()
.flatMap(s -> s.getMethods().stream())
.filter(p -> p.getModifiers().contains(JavaModifier.STATIC)
&& p.getName().equals(m.getName())
&& p.getRawParameterTypes().equals(m.getRawParameterTypes()))
.forEach(p -> events.add(SimpleConditionEvent.violated(m,
m.getFullName() + " hides " + p.getFullName())));
}
}
};
@ArchTest static final ArchRule noStaticHiding = classes().should(notHideStaticMethods);

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.

What to say in the interviewAbout 90 seconds aloud

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.
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/