Scrivio Interview library
LeadershipAdvanced6 min read · updated 12 November 2026

Should a shared library make its classes final by default? Argue both sides and state the policy you would adopt.

Yes for most classes, with deliberate exceptions. Final classes let library maintainers change internals without breaking unknown subclasses, and removing final later is compatible while adding it later is a breaking change. The cost is that some proxy-based frameworks, such as Spring's CGLIB proxies and JPA lazy-loading proxies, and some older mocking tools need non-final classes. My policy is final by default for value types and utilities, documented extension points where subclassing is intended, sealed classes for fixed hierarchies, and no final on classes the library's users must proxy.

Why this question is asked

Every shared library team eventually argues about this, often after a release where a harmless internal change broke a consumer's subclass. Interviewers ask senior engineers to argue both sides because the right answer depends on understanding what subclassing commits a library to, how Java frameworks use subclassing behind the scenes, and which choices can be undone later. In this answer, we will make the case for final by default, then the case against, look at the one asymmetry that settles most of the debate, and set out the policy, including where sealed classes fit.

1The Case for Final by Default

When a class can be subclassed, every subclass depends on more than its public methods. It depends on which methods call which other methods, on when they are called and on the order of calls. This is the fragile base class problem: a change that is invisible to normal callers, such as making addAll() stop calling add() internally, silently breaks a subclass that overrode add() to count elements.

Java
public class Inventory {
public void add(Item i) { items.put(i.id(), i); }
public void addAll(List<Item> list) { for (Item i : list) add(i); } // version 1
}
public class CountingInventory extends Inventory { // written by a consumer
int count;
@Override public void add(Item i) { count++; super.add(i); }
}
// version 1: addAll(3 items) -> count == 3
// version 2 changes addAll to items.putAll(...) for speed -> count == 0, consumer's bug report

If Inventory were final, the consumer would have used composition, wrapping an Inventory and counting in the wrapper, and the library could optimize addAll freely. A class that is open for extension must document its self-use, which methods call which, and keep that stable forever. Most library classes were never designed with that in mind, so leaving them open makes a promise nobody meant to make.

2The Case Against

Java frameworks often subclass our classes at runtime. Spring creates CGLIB subclass proxies for @Configuration classes and for beans that get @Transactional, @Async or other advice when they are not used through an interface; a final class cannot be proxied that way and fails at startup, and final methods are silently not intercepted. JPA providers such as Hibernate create lazy-loading proxies by subclassing entity classes, so entities and their methods should not be final. Kotlin, whose classes are final by default, ships an all-open compiler plugin for Spring and JPA for exactly this reason.

Mocking is the other cost. Older Mockito versions could not mock final classes. Since Mockito 5 the inline mock maker is the default and can mock final classes and methods, but teams on older tools, or with other frameworks, still hit the limit. And sometimes consumers have a legitimate need to adjust behaviour that the library did not anticipate; a final class forces them to wait for a release or to copy code.

3The Asymmetry That Settles Most Cases

Removing final from a class in a later release is a compatible change: every existing consumer still compiles and runs. Adding final later is a breaking change: any consumer who subclassed the class now fails to compile, or fails when the class is loaded, with a VerifyError saying it cannot inherit from a final class, if they were not recompiled. So final is the choice we can undo cheaply, and open is the choice we are stuck with.

That makes the default clear. When we are unsure whether a class should be extensible, we make it final. If a real need appears, we open it, and at that point we know what the extension point is for and can design and document it properly.

4The Policy

The policy has four parts. Value types and utilities, such as money amounts, identifiers, configuration objects and static helper classes, are final; most of them should be records or have private constructors anyway. Service classes intended to be used, not extended, are final, and consumers customize them through composition, interfaces or strategy objects the library accepts.

Where subclassing is intended, the class is a designed extension point: it documents which methods may be overridden, what they are called from, and which ones are final to protect invariants, and it gets tests that exercise a sample subclass. Abstract base classes for plugins are the typical case.

For a fixed family of types, sealed classes are the middle ground: the library declares exactly which classes may extend a type, so it keeps control of the hierarchy while still using inheritance, and consumers get exhaustive switch statements over it.

Finally, the library does not make final anything its users are expected to proxy: classes meant to be Spring beans with advice, or JPA entities provided by the library, stay open, and that is written down so a well-meaning cleanup does not break them.

Java
public sealed interface PaymentResult permits Approved, Declined, Pending {}
public record Approved(String authCode) implements PaymentResult {}
public record Declined(String reason) implements PaymentResult {}
public record Pending(Duration retryAfter) implements PaymentResult {}
String text = switch (result) { // exhaustive: no default needed
case Approved a -> "ok " + a.authCode();
case Declined d -> "declined: " + d.reason();
case Pending p -> "retry in " + p.retryAfter();
};
Step throughShould this library class be final?
  1. Must users proxy it (Spring advice, JPA entity)?
    YesLeave it open and document whyNoContinue
  2. Is it a value type or utility?
    Yesfinal, or a recordNoContinue
  3. Is extension a designed feature?
    YesOpen, document self-use, protect invariantsNoContinue
  4. Is the set of subtypes fixed?
    Yessealed with permitsNofinal by default

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

When in doubt, choose final: it is the choice we can undo compatibly.

5Follow-up questions

How do consumers customize a final class?

Through composition and the hooks the library provides: they wrap the object and delegate, pass strategy objects or callbacks the library accepts, or depend on an interface the final class implements and provide their own implementation. That keeps their code tied to the public contract instead of internal call order.

Is final by default a problem for testing consumers' code?

Less than it used to be. Mockito 5's default inline mock maker mocks final classes, and if the library exposes interfaces for its services, consumers can mock those instead. For value types, tests should use real instances anyway, which records make easy.

Would you enforce the policy automatically?

Yes, lightly. An ArchUnit or Error Prone rule can require that public classes in the library are final, sealed or annotated as an extension point, so every open class is a decision someone made and recorded rather than an accident.

6Conclusion

In this answer, we argued both sides of final by default. For it: final classes avoid the fragile base class problem and let maintainers change internals freely. Against it: Spring's CGLIB proxies, JPA lazy proxies and older mocking tools need non-final classes, and consumers lose a way to adjust behaviour. The asymmetry decides most cases, since removing final later is compatible and adding it is breaking, so the policy is final by default for value types, utilities and services, designed and documented extension points where subclassing is intended, sealed classes for fixed hierarchies, and open classes where users must proxy them.

What to say in the interviewAbout 90 seconds aloud

Open with

I would argue both sides and then use the compatibility asymmetry to decide. For final by default: every open class silently promises subclasses its internal self-use, which methods call which, so a harmless optimization like changing addAll to stop calling add can break a consumer's override.

Then, if they want more

That is the fragile base class problem, and final classes let maintainers change internals freely and push consumers to composition.

Against it: Spring creates CGLIB subclass proxies for configuration classes and for advised beans without interfaces, and JPA providers subclass entities for lazy loading, so final classes break those; older mocking tools could not mock finals, though Mockito 5 can by default; and consumers lose an escape hatch.

What settles it for me is that removing final later is a compatible change, while adding final later breaks anyone who subclassed. So my policy is final by default for value types, utilities and services, ideally records for values. Where extension is intended, the class is a designed extension point with documented overridable methods and protected invariants.

For fixed hierarchies I use sealed types with permits, which also give exhaustive switches. And anything users are expected to proxy, like library-provided entities or advised beans, stays open and is documented as such. I would back it with an ArchUnit rule so every open public class is a recorded decision.

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 "Should a shared library make its classes final by default? Argue both sides and state...": https://scrivio-interview-library.netlify.app/java/classes-and-objects/57a16ae083/