Quick revision
The short answer
I would treat it as a multi-year evolution, not a rewrite. The reason to move is real: inheritance across library boundaries couples users to the library's implementation details and blocks the library from evolving, while composition and small interfaces let parts change independently and are easier to test. The plan is to provide composition-based alternatives in the shared libraries before deprecating any base class, migrate opportunistically when code is touched, using automated refactoring where it is safe, never fund rewrites for style alone, and measure progress with dependency analysis such as the number of classes still extending deprecated base classes.
What to remember
In this answer, we saw why inheritance across library boundaries freezes libraries and makes upgrades expensive, while composition and small interfaces let parts change independently and are easier to test. The plan to steer a large organization is to build composition-based alternatives in shared libraries before deprecating base classes, migrate opportunistically when code is touched, automate mechanical changes with refactoring recipes, fund dedicated work only for concrete costs such as blocked upgrades, and measure progress with dependency analysis, such as the number of classes still extending deprecated base classes, enforced in CI so it can only shrink.
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.
I would treat it as a multi-year evolution, not a rewrite. The reason to move is real: inheritance across library boundaries couples users to the library's implementation details and blocks the library from evolving, while composition and small interfaces let parts change independently and are easier to test. The plan is to provide composition-based alternatives in the shared libraries before deprecating any base class, migrate opportunistically when code is touched, using automated refactoring where it is safe, never fund rewrites for style alone, and measure progress with dependency analysis such as the number of classes still extending deprecated base classes.
Why this question is asked
Large Java organizations carry code from every era: servlets extending HttpServlet, DAOs extending support classes, tests extending TestCase or a company BaseIntegrationTest, and internal frameworks built around abstract base classes. Modern Java and modern Spring favour constructor injection, small interfaces, functions and records. A senior architect is often asked to move hundreds of repositories in that direction without stopping product work. Interviewers ask this question to see how we turn a design principle into an organizational change: the argument, the order of steps, the tooling, the funding decision and the measures. In this answer, we will explain why the shift matters, describe the order of steps, show what a migration looks like in code, decide what gets funded, and define how we measure progress.
1Why the Shift Matters Beyond Style
The case has to be made in terms of cost, not taste. When an application class extends a library's base class, it inherits not only the public methods but the library's internal behaviour: which methods call which, in what order, with what state. Those details become a contract by accident. The library team then cannot change them without breaking users, so libraries freeze, and upgrades of the framework underneath become expensive projects.
Composition reverses that. An application object that holds a JdbcClient or an HttpClient through constructor injection depends only on that object's public interface. The library can change its internals freely, and the application can be tested by passing a fake. Small interfaces, often functional ones, let teams plug behaviour into frameworks without being subclasses of them. The organizational benefit is faster, cheaper upgrades, which is something leadership cares about.
2Step One: Provide Alternatives Before Deprecating Anything
Teams cannot migrate away from a base class if there is nothing to migrate to. So the first investment is in the shared libraries: for every widely extended base class, the platform team builds a composition-based alternative and documents it with a before-and-after example. Only when the alternative is released, tested and used by a pilot team do we mark the base class @Deprecated, with a Javadoc link to the replacement and a removal date that is at least a year away.
Notice that the new version has no hidden behaviour. Everything the DAO does is visible in its own code, and in a test we can construct it with a JdbcClient pointing at a test database without inheriting anything. The platform team's job is to make the new path at least as convenient as the old one, or adoption will stall.
3Step Two: Migrate Opportunistically, Automate What Is Safe
The most important funding rule is that we do not pay for rewrites for style alone. Code that works, is rarely changed and is not blocking an upgrade can keep extending a deprecated base class until it is retired. Instead, teams migrate a class when they are already changing it for a feature or a fix, the boy scout rule applied with a guide in hand. That spreads the cost across normal work and puts it where the code is most active, which is also where the benefit is highest.
Where a change is mechanical, we automate it. Tools such as OpenRewrite can apply recipes across hundreds of repositories, for example migrating JUnit 4 tests to JUnit 5, which removes many test base classes and runners, or replacing deprecated APIs with their successors. The platform team writes or adopts recipes, runs them in a pilot repository, and then opens the pull requests automatically for everyone else. Teams only review and merge. Changes that need judgment, such as untangling a deep hierarchy, stay manual and happen when the code is touched.
- Is a composition-based alternative released?YesCheck how the class is changedNoPlatform team builds it first
- Is the change mechanical?same structure, different APIYesAutomated recipe, team reviews the PRNoCheck activity
- Is the code being changed anyway?feature, fix or upgrade workYesMigrate as part of that changeNoLeave it until touched or retired
- Blocks a framework upgrade?YesFund a targeted migrationNoNo dedicated funding
4 steps in order. Play them, or step through them one at a time.
4Step Three: Measure Progress With Dependency Analysis
A multi-year change needs a number that everyone can see. The simplest is the count of classes, per repository and in total, that extend or implement each deprecated type. It can be computed in CI with an ArchUnit test or a small bytecode scan, and published on a dashboard each week. A rising curve of migrated classes and a falling curve of remaining subclasses tell leadership the investment is working; flat lines tell us where teams need help.
Notice that the rule also stops the count from growing: new code outside the legacy package cannot extend the deprecated class. That is often the most valuable effect, because it guarantees the problem shrinks. Alongside the count, we watch the outcomes we actually care about, such as how long framework upgrades take and how many incidents trace back to base class changes.
5Keeping People on Board
Changes like this fail more often from people than from technology. Developers who have used the base classes for fifteen years know them well and see little reason to change. So I would lead with concrete examples from our own code, the bug that a base class change caused, the upgrade it delayed, rather than with principles. I would invite senior engineers from product teams into the design of the alternatives, so they own them, and celebrate teams that migrate. And I would be honest that inheritance still has its place: designed-for-extension classes inside one team's code, and true is-a relationships, are fine. The goal is to stop inheriting across team and library boundaries, not to ban a keyword.
6Follow-up questions
How do you handle teams that refuse to migrate?
First find out why. Often the alternative is missing a feature they rely on, which is useful feedback for the platform team. If the alternative is complete, the deprecation timeline does the rest: after the removal date, the old base class stays only in an unsupported legacy artifact, and teams that keep using it own the cost of their own upgrades. Most teams move well before that point if the migration path is easy.
What would make you fund a dedicated migration project?
A concrete business cost: a base class blocking a framework upgrade with security fixes, such as moving to a new Spring or Jakarta EE major version, or a base class causing repeated incidents. Then a targeted project for those classes is justified, with a clear end. Style alone never justifies one.
7Conclusion
In this answer, we saw why inheritance across library boundaries freezes libraries and makes upgrades expensive, while composition and small interfaces let parts change independently and are easier to test. The plan to steer a large organization is to build composition-based alternatives in shared libraries before deprecating base classes, migrate opportunistically when code is touched, automate mechanical changes with refactoring recipes, fund dedicated work only for concrete costs such as blocked upgrades, and measure progress with dependency analysis, such as the number of classes still extending deprecated base classes, enforced in CI so it can only shrink.
Open with
I would frame this as a cost problem, not a style debate. When application classes extend library base classes, they depend on the library's internal behaviour, so the library cannot evolve without breaking them, and every framework upgrade becomes a big project.
Then, if they want more
Composition and small interfaces let both sides change independently, and they make testing easier because dependencies can be replaced with fakes. The plan has a few rules.
First, the platform team builds composition-based alternatives in the shared libraries, for example DAOs that take an injected JdbcClient instead of extending a company base DAO, documents them with before-and-after examples, and only then deprecates the base classes with a removal date at least a year out.
Second, migration is opportunistic: teams move a class when they are already changing it, and we do not fund rewrites just for style. Where the change is mechanical, like JUnit 4 to JUnit 5, we use automated refactoring such as OpenRewrite recipes and send the pull requests to teams.
Dedicated funding is only for concrete costs, like a base class blocking a security upgrade. Third, we measure: an ArchUnit rule or bytecode scan in CI counts the classes still extending each deprecated base class, publishes it per repository, and prevents new subclasses, so the number can only go down.
And I would keep people on board with real examples from our own incidents and by involving senior engineers in designing the alternatives.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- JUnit 4, JUnit 5
- 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 "Java frameworks of the 2000s relied heavily on inheritance (base classes for servlets...": https://scrivio-interview-library.netlify.app/java/oop-principles/0d14f0e4c5/