Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

You maintain a widely used internal interface PaymentGateway. You must add a capability for refunds. Compare a new default method, a new interface, and a new major version.

A new default method that throws UnsupportedOperationException is compatible but hides the problem until a refund is attempted at run time. A separate RefundCapable interface keeps PaymentGateway unchanged and lets callers check by type which gateways can refund. A new major version with an abstract refund method is the cleanest model but forces every implementer to change in a coordinated release. Which one is right depends mostly on who implements PaymentGateway, not on who calls it, and I would usually choose the separate interface first.

Why this question is asked

A PaymentGateway interface used across a company is a contract between two groups: the teams who call it to take payments and the teams who implement it for each payment provider. Adding refunds touches both groups, and the way we add it decides who pays the cost and when failures appear. Interviewers ask this design question because it tests whether we can compare options by their consequences rather than by taste. In this answer, we will set up the situation, look at each of the three options with code and its real cost, see why implementers versus callers is the deciding factor, and finish with the rollout: compatibility checks and a migration window.

1Setting Up the Situation

Let us give the problem some numbers. PaymentGateway has two methods, authorize and capture. It is called from about 120 services through a shared client library, and it is implemented by 9 adapters, one per payment provider, owned by four different teams. Six providers support refunds today; three do not and will not for at least a year.

We want callers to be able to refund a payment when the provider supports it, and to know clearly when it does not. We also want no adapter to break on the day we release, because those four teams have their own plans.

2Option 1: A Default Method That Throws

The quickest change is a default method. Every existing adapter compiles and runs unchanged, and adapters that support refunds override it.

Java
public interface PaymentGateway {
AuthResult authorize(AuthRequest r);
CaptureResult capture(CaptureRequest r);
default RefundResult refund(RefundRequest r) {
throw new UnsupportedOperationException("refunds not supported by " + getClass().getSimpleName());
}
}
gateway.refund(req); // for a provider without refunds: UnsupportedOperationException at run time

This is fully compatible, and that is its only real strength. The type system now says every gateway can refund, which is false for three of the nine. Callers find out by catching an exception in production, and the compiler cannot help them handle the unsupported case. We could add a default supportsRefunds() returning false, but that is a boolean the caller must remember to check, so the same mistake is still possible. The JDK does this with optional collection operations, and it is widely seen as a compromise rather than a model to copy.

3Option 2: A Separate RefundCapable Interface

The second option leaves PaymentGateway alone and adds a new interface for the new capability. Adapters that can refund implement both; callers ask whether a gateway has the capability.

Java
public interface RefundCapable {
RefundResult refund(RefundRequest r);
}
final class StripeLikeAdapter implements PaymentGateway, RefundCapable { ... }
// Caller
if (gateway instanceof RefundCapable refunds) {
return refunds.refund(req); // only reachable for capable gateways
}
return RefundResult.manualRefundRequired(); // the unsupported case is visible in code
// Or, better, let the registry answer the question once
Optional<RefundCapable> refunds = gateways.refundsFor(providerId);

Notice that the unsupported case is now an ordinary branch the caller writes, not an exception it forgets to catch. Nothing that exists today changes, so the release is binary and source compatible for all 9 adapters and all 120 callers. The costs are smaller but real. instanceof checks can spread through calling code, which is why the registry method is the better caller API. And any wrapper class that implements PaymentGateway by delegation, such as a logging or metrics decorator, hides the new interface unless it is updated too, so we must find and fix those wrappers.

4Option 3: A New Major Version With an Abstract Method

The cleanest model is to release PaymentGateway 2.0 with refund as an abstract method, perhaps returning a result that can say "not supported". Every adapter must then implement it, and the compiler checks that they did.

Java
public interface PaymentGateway { // 2.0
AuthResult authorize(AuthRequest r);
CaptureResult capture(CaptureRequest r);
RefundResult refund(RefundRequest r); // every implementer must decide
}
// adapter compiled against 1.x running with 2.0: AbstractMethodError when refund is called
// adapter rebuilt against 2.0 without the method: compile error
Comparing the three options
OptionBreaks anything today?Where failures appearMain cost
Default that throwsNoRun time, at refundA type that lies about capability
RefundCapable interfaceNoCompile time, in caller branchesCapability checks; update wrappers
Major version, abstract methodYes, every implementerCompile time, in adaptersCoordinated migration
The separate interface moves failures to compile time without breaking anyone.

The price is coordination. All four adapter teams must ship new versions before callers can rely on 2.0, and during the transition services may have the old and new library on different paths. With 9 adapters this is a few weeks of work; with 60 implementations across 30 teams it could take a year. A major version is right when the old model is genuinely wrong and we are ready to pay for a migration, not as the default way to add a feature.

5Who Implements Versus Who Calls

The most important question is not how many services use the interface, but how many of them implement it. Adding a method is free for callers, who simply do not call it yet. It is expensive for implementers, who must write it. An interface called by 120 services and implemented by 3 adapters inside one team can take a new abstract method in an afternoon. An interface implemented by dozens of teams is effectively frozen, and should grow only through new interfaces or carefully chosen defaults.

In our situation, 9 adapters across four teams sits in the middle. That is why I would choose RefundCapable now: it delivers refunds for the six capable providers immediately and breaks nobody. If, a year later, all providers support refunds, we can plan a 2.0 that merges the interfaces, with time to coordinate.

Step throughChoosing how to add a capability
  1. Do all implementations support it?
    YesConsider an abstract methodNoUse a separate capability interface
  2. Are implementers few and in one team?
    YesAdd it in a coordinated releaseNoPlan a major version with a window
  3. Run japicmp or Revapi on the releasefail minor releases with breaking changes

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

6Rolling It Out: Checks and a Migration Window

Whatever option we pick, the release goes through a compatibility check such as japicmp or Revapi, which confirms that a minor release contains no binary or source incompatible change. We announce the change with the release notes, the new caller API (the registry method), and the list of wrappers that must forward the new interface.

If we do go to a major version later, we give a migration window, for example two quarters, during which 1.x is supported with fixes, the 2.0 adapters are ready, and a dashboard shows which services still depend on 1.x. The old version is retired only when that number reaches zero or the remaining owners have agreed a date.

7Follow-up questions

Would a Spring proxy hide the RefundCapable interface?

Usually not. Spring Boot creates class-based CGLIB proxies by default, which are subclasses of the adapter, and a JDK dynamic proxy implements all the target bean's interfaces, so a transactional or cached adapter still passes the instanceof check. Hand-written decorators are the usual problem, because they implement only the interfaces their author listed.

Why not return Optional<RefundResult> from a default method instead of throwing?

That is better than throwing, because the caller must handle the empty case. It still mixes two meanings in one value, "this provider cannot refund" and "this refund produced nothing", and it still claims every gateway is refund-capable. A capability interface or a registry query keeps the question "can this gateway refund?" separate from the result of a refund.

When is the default method option the right choice?

When there is a sensible default behaviour, not an exception. If refunds could be implemented in terms of existing methods for most providers, a working default that some adapters override is a good, compatible design.

8Conclusion

In this answer, we compared three ways to add refunds to a shared PaymentGateway interface. A throwing default method is compatible but turns a missing capability into a run-time exception. A separate RefundCapable interface keeps the old contract and makes capability a type that callers can check. A major version with an abstract method is the cleanest but costs a coordinated migration. The number of implementers, more than the number of callers, decides which cost is acceptable, and compatibility checks plus a migration window keep the rollout safe.

What to say in the interviewAbout 80 seconds aloud

Open with

I would compare the three by where failures show up and who pays. A default refund method that throws UnsupportedOperationException is perfectly compatible, nothing breaks on release, but the type now claims every gateway can refund, and callers only discover otherwise from an exception in production.

Then, if they want more

A separate RefundCapable interface leaves PaymentGateway untouched, so all existing adapters and callers keep working, and capability becomes a type: a caller checks it, ideally through a registry method that returns an Optional, so the unsupported path is written explicitly. The cost is updating decorators that would otherwise hide the new interface.

A 2.0 with an abstract refund method is the cleanest model and the compiler makes every adapter decide, but it forces a coordinated release across every implementing team. The deciding factor for me is who implements the interface versus who only calls it. Adding methods is free for callers and expensive for implementers.

With several adapter teams and some providers that cannot refund, I would ship RefundCapable now, run japicmp or Revapi to prove the release is compatible, and only consider a major version later, with a migration window and tracking of who still depends on 1.x.

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 "You maintain a widely used internal interface PaymentGateway. You must add a capability...": https://scrivio-interview-library.netlify.app/java/interfaces-and-abstract-classes/4854392bda/