Quick revision
The short answer
In a shared domain library, lookup methods that may find nothing return Optional, methods that return collections return empty collections, and parameters and fields never use Optional; any other value that can be absent carries the organisation's @Nullable annotation. Changing a return type from T to Optional<T> breaks both source and binary compatibility, so we add a new method, deprecate the old one and remove it only in a major version. The rule is written in the library guide and checked in API review and in CI.
What to remember
In this answer, we saw a three-part rule for a shared library: Optional for lookups that may find nothing, empty collections for many results, never Optional in parameters or fields, and nullness annotations for everything else that may be absent. Changing a return type from T to Optional<T> breaks source compatibility and, because the return type is part of the method descriptor, binary compatibility too, so we add a new method, deprecate the old one and remove it in a major version. A short guide, API review, NullAway, ArchUnit and a binary compatibility checker keep the rule consistent across all twenty teams.
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.
In a shared domain library, lookup methods that may find nothing return Optional, methods that return collections return empty collections, and parameters and fields never use Optional; any other value that can be absent carries the organisation's @Nullable annotation. Changing a return type from T to Optional<T> breaks both source and binary compatibility, so we add a new method, deprecate the old one and remove it only in a major version. The rule is written in the library guide and checked in API review and in CI.
Why this question is asked
A library used by twenty teams is a contract with twenty codebases that upgrade at different times. Its signatures cannot be changed casually, and inconsistencies in it spread into every consumer. Interviewers ask this question to see whether we can write a clear rule, apply it to real interfaces, and handle the harder part: evolving those signatures without breaking teams that are not ready. In this answer, we will set out the rule for Optional, apply it to a repository and a service interface, see exactly why changing a return type breaks consumers, and then cover how the rule is documented and enforced.
1The Rule in Three Sentences
First, a method that looks something up and may legitimately find nothing returns Optional<T>; a method that returns many things returns a collection, which is empty when there is nothing. Second, Optional never appears as a parameter type, a field type or inside a collection. Third, any other reference that may be null is annotated with the organisation's chosen nullness annotation, and everything else is non-null by default.
The rule is short on purpose. Twenty teams will apply it without reading a long document, so it has to fit in the head. Each part has a reason: Optional makes expected absence visible in lookups; empty collections already express "nothing"; and Optional parameters and fields add wrapping, allocation and serialization problems without preventing null.
2Applying It to the Interfaces
Here is how the rule shapes a repository and a service in the shared library.
Notice the record: the component itself is a nullable String, because records and persistence frameworks handle plain fields well, and an accessor returns Optional for callers who want it. The service also shows that not every lookup returns Optional. getCustomer is used where the id comes from our own system and absence would be a bug, so it throws instead.
3Why Changing T to Optional<T> Breaks Consumers
Suppose version 1 has Customer findByEmail(EmailAddress) returning null, and version 2 changes it to return Optional<Customer>. The source break is obvious: consumer code that assigns the result to a Customer variable no longer compiles. The binary break is less obvious and more dangerous.
In compiled bytecode, a method call names the method by its descriptor, which includes the return type. A consumer service compiled against version 1 calls findByEmail with a descriptor that ends in Customer. If that service runs with version 2 on its classpath, perhaps because another dependency pulled the newer library in, the JVM looks for a method returning Customer, finds only one returning Optional, and throws NoSuchMethodError at the moment of the call, in production.
4 messages in order. Play them, or step through them one at a time.
The return type is part of the method descriptor, so changing it removes the old method for compiled callers.
4Evolving a Signature Safely
The safe path is to add, deprecate and remove later. We add a new method with a new name and the new return type, keep the old method working, mark it deprecated with a pointer to the replacement, and remove it only in the next major version.
Teams then migrate at their own pace during the 4.x line, with compiler warnings from forRemoval = true showing them where. If the interface has implementations outside the library, the new method can be a default method that delegates to the old one, so implementers do not break either. In version 5.0, the old method is removed and the new name can stay, since renaming it back would be another breaking change.
5Documenting and Enforcing the Rule
The rule goes into the library guide as a short page with the code above as its example, because developers copy examples far more reliably than they follow prose. API review is the human gate: any pull request that changes a public signature in the shared library needs approval from a library owner, who checks it against the rule.
Machines do the rest. NullAway in the library build enforces the nullness annotations, an ArchUnit test fails if any public method takes an Optional parameter or any class has an Optional field, and a binary compatibility checker such as japicmp or Revapi compares each build against the last release and fails on a breaking change in a minor version. That last check catches exactly the T to Optional<T> mistake before it reaches any of the twenty teams.
6Follow-up questions
Why not let each team choose Optional or @Nullable for its own methods?
Because consumers see the whole library, not one team's part of it. Mixed conventions mean every caller has to check each method's style before using it, and Kotlin consumers get Optionals in some places and nullable types in others. A single rule makes the library predictable.
Is adding a method to an interface always safe?
Adding an abstract method breaks every implementation outside the library, because those classes no longer compile and fail at runtime with AbstractMethodError if the method is called. Adding a default method is safe for most cases, which is why new methods on shared interfaces should come with a default implementation when external implementations exist.
How long should a deprecated method stay?
At least one full minor-release cycle, and in practice until the next major version, with a published removal date. Usage data helps: if a dependency scan shows no service still calls the old method, it can be removed with confidence.
7Conclusion
In this answer, we saw a three-part rule for a shared library: Optional for lookups that may find nothing, empty collections for many results, never Optional in parameters or fields, and nullness annotations for everything else that may be absent. Changing a return type from T to Optional<T> breaks source compatibility and, because the return type is part of the method descriptor, binary compatibility too, so we add a new method, deprecate the old one and remove it in a major version. A short guide, API review, NullAway, ArchUnit and a binary compatibility checker keep the rule consistent across all twenty teams.
Open with
My rule would fit in three sentences. Lookup methods that can legitimately find nothing return Optional, and anything returning many results returns a collection that's empty when there's nothing.
Then, if they want more
Optional is never used for parameters, fields or collection elements. Anything else that may be null carries our standard nullness annotation, with packages marked so non-null is the default. Where the id comes from our own system and absence is a bug, I'd offer a getX that throws instead. The harder part is change.
Switching a return type from T to Optional of T breaks source compatibility, and also binary compatibility, because the return type is part of the method descriptor in bytecode. A service compiled against the old version that ends up with the new jar on its classpath fails with NoSuchMethodError at the call.
So we never change it in place: we add a new method, mark the old one Deprecated with forRemoval and a pointer to the replacement, give teams the whole minor-version line to migrate, and remove it in the next major version.
For consistency, the rule goes in the library guide with examples, every public signature change goes through API review by a library owner, and CI enforces it with NullAway, an ArchUnit test against Optional parameters and fields, and japicmp or Revapi to block breaking changes in minor releases.
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
- java.util.stream, package documentation, java.util.function, package 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 "You are designing the repository and service interfaces of a shared domain library used...": https://scrivio-interview-library.netlify.app/java/optional/6c298aaf31/