Quick revision
The short answer
A deep hierarchy can only share behaviour along one branch, so a payment type that needs pieces from two sibling branches forces duplication or a fragile sixth level. I would identify the concerns that actually vary, such as validation, fees and settlement, extract each into its own strategy interface, and compose a processor from one strategy per concern, so the new type simply picks its combination. The refactoring goes step by step behind characterization tests, introducing the strategies inside the existing classes first and flattening the hierarchy afterwards, with sealed interfaces where the set of payment types is closed.
What to remember
In this answer, we saw that a five-level payment hierarchy cannot combine behaviour from sibling branches, so a new type that needs card validation and instant settlement forces duplication. We identified the varying concerns, validation, fees and settlement, extracted each into a strategy interface and composed processors from them, which made the new type a single combination. We refactored safely with characterization tests, extracting strategies inside the old classes before flattening the hierarchy one payment type at a time, and used sealed interfaces where the set of payment types is closed.
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.
A deep hierarchy can only share behaviour along one branch, so a payment type that needs pieces from two sibling branches forces duplication or a fragile sixth level. I would identify the concerns that actually vary, such as validation, fees and settlement, extract each into its own strategy interface, and compose a processor from one strategy per concern, so the new type simply picks its combination. The refactoring goes step by step behind characterization tests, introducing the strategies inside the existing classes first and flattening the hierarchy afterwards, with sealed interfaces where the set of payment types is closed.
Why this question is asked
Payment code tends to grow by subclassing: someone needs card payments that are slightly different, so they extend CardProcessor and override a hook, and five years later there are five levels and a new requirement that fits nowhere. This is a common real situation, and interviewers ask about it to see whether we can diagnose why the hierarchy fails, design a better structure, and, most importantly for a senior role, change a critical, money-moving system safely. In this answer, we will picture the legacy hierarchy and the new requirement, see why inheritance cannot combine sibling branches, design strategies per concern, walk through the refactoring step by step, and decide where sealed types fit.
1The Legacy Hierarchy and the New Requirement
Here is a simplified version of such a hierarchy. Each level overrides hooks such as validate(), calculateFee() and settle() from the level above.
Notice where the new type's behaviour lives. Card validation is in CardProcessor, on one branch. Instant settlement is in SepaInstantProcessor, on the other. Java has single inheritance of classes, so the new class can extend only one of them. Whichever it picks, it must copy the other branch's code, and copied payment logic is a source of real money bugs, because fixes to one copy do not reach the other.
2Why Inheritance Cannot Combine Branches
A class hierarchy is a tree organised along one dimension, here the payment method. But a processor really varies along several independent dimensions: how input is validated, how fees are computed, how money is settled. A tree can only represent combinations that happen to line up with its branches. Every new combination either duplicates code or adds a level that overrides hooks from the level above, which is how hierarchies reach five levels in the first place.
Deep hierarchies also hide behaviour. To know what InternationalCreditCardProcessor does when it settles, we must read five classes and track which level last overrode each hook and whether it called super. That makes every change risky, which is exactly what we cannot afford in payment code.
| Payment type | Validation | Fee | Settlement |
|---|---|---|---|
| Credit card | card | percentage | batch, next day |
| International card | card | percentage + FX | batch, next day |
| SEPA instant | IBAN | flat | instant |
| Card-funded instant payout (new) | card | flat | instant |
3Designing a Strategy for Each Concern
The fix is to model each varying concern as its own interface, a strategy, and to build a processor by composing one strategy per concern. A strategy is an object that encapsulates one way of doing something behind a small interface, so it can be swapped without changing the code that uses it.
The new payment type is now one line of wiring. Notice that each strategy is small and testable on its own: CardValidator can be tested with card numbers, InstantSettlement with a fake bank client, without building a whole processor. The processor itself is final and has one obvious flow, so there is no question of which level overrides what. In a Spring application, each combination would be a bean definition, and the processors could be looked up by payment type from a map.
4Refactoring Step by Step
A payment system cannot be rewritten in one go. The first step is characterization tests: tests that record what the current code actually does for a wide set of inputs, fees to the cent, validation errors, settlement calls, whether or not that behaviour is ideal. They are the safety net for every later step, and any difference they show must be a decision, not an accident.
Next, introduce the strategies inside the existing classes. CardProcessor's validate() body moves into a CardValidator class, and CardProcessor.validate() just calls it. The behaviour is identical and the tests stay green, but the logic is now reusable. We repeat this for fees and settlement at every level. Then we can build the new card payout from strategies right away, without waiting for the rest of the migration.
Finally, we flatten. Once each legacy class only delegates to strategies, we replace it with a PaymentProcessor composed of the same strategies, switch callers over one payment type at a time, ideally behind a feature flag with the old path still available, and delete the empty classes. Each step is a separate, reviewable change that ships on its own.
- Write characterization testsfees, errors and settlement calls for each existing type
- Extract strategies inside old classesold hooks delegate to
CardValidator,FlatFee, ... - Build the new type from strategiesships without touching the hierarchy
- Switch one payment type at a timecomposed processor behind a feature flag
- Delete the empty subclassestests still green, hierarchy flattened
5 steps in order. Play them, or step through them one at a time.
5Where Sealed Interfaces Fit
If the business supports a fixed, deliberate set of payment types, the type itself can be modelled as a sealed interface of records, such as sealed interface PaymentType permits Card, SepaInstant, CardPayout. A factory can then use an exhaustive switch to build the right processor for each type, and adding a payment type makes the compiler point at every switch that must handle it. If instead new providers and methods arrive all the time, for example through partner integrations, the strategies stay open interfaces and new types register their own combination. In both cases, the behaviour lives in composed strategies rather than in a deep tree.
6Follow-up questions
How do you make sure the refactored fees are exactly the same?
Characterization tests cover the known cases, and for money I would add a comparison run: for a period, compute fees with both the old and new code in production, use the old result, and log any difference. Once the difference count has been zero for a meaningful volume of real payments, we switch to the new code. This is cheap to build and catches cases the tests did not imagine.
Is a five-level hierarchy ever justified?
Rarely in business code. Deep hierarchies can work in frameworks where each level adds a real, documented abstraction, such as the collection classes. In domain code, depth usually means several independent concerns have been forced into one tree. A useful warning sign is a subclass that overrides a hook only to undo what its parent did.
7Conclusion
In this answer, we saw that a five-level payment hierarchy cannot combine behaviour from sibling branches, so a new type that needs card validation and instant settlement forces duplication. We identified the varying concerns, validation, fees and settlement, extracted each into a strategy interface and composed processors from them, which made the new type a single combination. We refactored safely with characterization tests, extracting strategies inside the old classes before flattening the hierarchy one payment type at a time, and used sealed interfaces where the set of payment types is closed.
Open with
The core problem is that a class hierarchy shares behaviour along one dimension only, here the payment method, while processors actually vary along several independent concerns: validation, fees and settlement.
Then, if they want more
The new type needs card validation from one branch and instant settlement from another, and with single inheritance it can only extend one, so we would duplicate payment logic, which is dangerous. My approach is to identify those varying concerns and extract each into its own small strategy interface, like PaymentValidator, FeePolicy and Settlement.
A processor becomes a final class that composes one of each and runs a simple flow, so each payment type is a combination: the new card payout is card validation, a flat fee and instant settlement, with no new class at all. For the migration I would go step by step.
First characterization tests that pin current behaviour, fees to the cent and settlement calls. Then extract strategies inside the existing classes, so the old hooks just delegate and nothing changes for callers.
Then build the new type from strategies, and switch the existing types to composed processors one at a time behind a flag, maybe with a shadow comparison of fees, and finally delete the empty subclasses.
If the set of payment types is closed, I would model it as a sealed interface so a factory switch is exhaustive and the compiler finds every place to update.
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 codebase has a five-level class hierarchy for payment processors with...": https://scrivio-interview-library.netlify.app/java/oop-principles/5ba56f4ff6/