Quick revision
The short answer
We give the base builder a self type: abstract static class Builder<T extends Builder<T>> whose setters return self(), an abstract method each subclass implements as return this. HttpRequest.Builder then extends Request.Builder<HttpBuilder>, so timeout() returns HttpBuilder and callers can chain base and subclass methods in any order. The cost is harder signatures, confusing compiler errors and room for raw-type misuse, so for a shared library with only a few subclasses, separate builders or covariant overrides are often simpler; the pattern earns its place in deep or open hierarchies.
What to remember
In this answer, we built a self-typed builder hierarchy: Request.Builder<T extends Builder<T>> returns self() from each setter, and HttpBuilder extends Request.Builder<HttpBuilder> so base and subclass methods chain in any order. We saw its costs, harder signatures, confusing compiler errors, raw-type and wrong-argument mistakes, and generics spreading through every level, and compared it with separate builders and covariant overrides. For a small, closed hierarchy in a shared library the simpler designs usually win, and the recursive pattern is best reserved for deep or open hierarchies.
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.
We give the base builder a self type: abstract static class Builder<T extends Builder<T>> whose setters return self(), an abstract method each subclass implements as return this. HttpRequest.Builder then extends Request.Builder<HttpBuilder>, so timeout() returns HttpBuilder and callers can chain base and subclass methods in any order. The cost is harder signatures, confusing compiler errors and room for raw-type misuse, so for a shared library with only a few subclasses, separate builders or covariant overrides are often simpler; the pattern earns its place in deep or open hierarchies.
Why this question is asked
Builders are everywhere in Java libraries, and inheritance between them is where they get hard. Without care, calling a base-class setter returns the base builder type, and the next subclass method no longer compiles. Interviewers ask this design question to see whether we can apply recursive generics to a real problem and, just as important, whether we can judge when the solution is too clever for the people who will use it. In this answer, we will reproduce the chaining problem, build the recursive builder step by step, see how callers use it, look at what it costs, and compare it with simpler designs before choosing.
1The Problem With Inherited Builders
A builder collects settings through chained method calls and creates the object at the end with build(). If Request has a timeout and HttpRequest adds headers, the natural first attempt is to let the HTTP builder extend the base builder. It breaks as soon as a caller calls a base method first.
Notice that the object really is an HttpRequest.Builder, but the static type returned by timeout() is Request.Builder, and the compiler only allows methods of the static type. We need timeout() to return whatever builder type the caller has.
2Building the Self-Typed Builder
The fix gives the base builder a type parameter that stands for the concrete builder, often called the self type. The bound T extends Builder<T> says T must itself be one of these builders. Base setters return T, and because the base class cannot cast this to T safely, it asks the subclass for it through an abstract self() method.
- Request.Builder<T extends Builder<T>>timeout(d) returns T,
self()is abstract - HttpBuilder extends Request.Builder<HttpBuilder>T is
HttpBuilder - timeout(d) as seen by callersreturns
HttpBuilder - header(k, v)subclass method, also returns
HttpBuilder - build()covariant return:
HttpRequest
5 layers, from the top down. Step through them one at a time.
The key line is HttpBuilder extends Request.Builder<HttpBuilder>: the subclass passes itself as T. Every base method declared to return T now returns HttpBuilder for this subclass, and self() supplies this with the right static type, so there is no unchecked cast anywhere.
3Chaining in Any Order
Callers can now mix base and subclass methods freely, and build() returns the precise type thanks to its covariant override.
A deeper subclass, say an AuthHttpBuilder, would need HttpBuilder to be generic too, HttpBuilder<T extends HttpBuilder<T>>, with a concrete leaf class at the bottom. That is the point at which the pattern starts to spread through every level of the hierarchy.
4What the Pattern Costs
The signatures are harder to read. A user browsing the Javadoc sees Builder<T extends Builder<T>> and timeout returning T, and must work out what T is. Compiler errors become confusing: misuse produces messages about inference variables and incompatible bounds instead of a simple missing method.
Raw types are an easy mistake. If someone declares a variable as a raw Request.Builder, without a type argument, the chain silently falls back to erased types: timeout() now returns the erased bound, a raw Request.Builder, so the next subclass call fails to compile or needs a cast, and the compiler only warns about raw types. Someone writing a new subclass can also pass the wrong type argument, for example class FtpBuilder extends Request.Builder<HttpBuilder>, and then gets an error in self() about incompatible types that takes a while to understand. Finally, every intermediate level that can itself be extended must carry the self type parameter, so the generics leak into every class of the hierarchy.
5Simpler Alternatives
For a hierarchy with a couple of subclasses, two simpler designs work well. The first is separate builders with no inheritance between them: each request type has its own builder with its own timeout method, perhaps sharing a small internal helper for common fields. There is a little duplication, but every signature is obvious.
The second is covariant overrides: the subclass builder overrides each base setter just to narrow the return type.
| Design | Best when | Main cost |
|---|---|---|
| Separate builders | 2 or 3 types, few shared fields | some duplicated setters |
| Covariant overrides | few base setters, shallow hierarchy | one override per base setter |
| Recursive generics | deep or open hierarchy, many setters | hard signatures, odd errors |
| Code generation | many types, uniform builders | build tooling, generated code |
This costs one short override per base method, which is cheap when the base builder has three setters and unattractive when it has thirty.
6What I Would Choose for a Shared Library
For a shared library, the audience matters most: hundreds of developers will read the signatures, and most will never write a subclass. If the hierarchy is closed and small, say Request with HttpRequest and GrpcRequest, I would use covariant overrides or separate builders, because callers see plain types and errors make sense. If the library is designed to be extended by other teams, or the hierarchy is genuinely deep with many shared setters, the recursive builder is worth it, and I would hide its complexity: provide concrete leaf builders through static factory methods such as HttpRequest.builder(), so callers never write a type argument, document the pattern for subclass authors, and add a test for each builder that checks a full chain compiles in both orders.
7Follow-up questions
Why is self() needed instead of casting this to T?
A cast (T) this compiles with an unchecked warning and would be wrong if a subclass passed the wrong type argument. self() lets each concrete subclass return this with its own static type, so the compiler checks it and no warning appears.
Can records or Lombok help?
Lombok's @SuperBuilder generates exactly this recursive pattern, which removes the boilerplate but keeps the complex signatures in the generated API. Records have no inheritance, so with records we would use separate builders.
What does a caller see if they declare the builder with a wildcard?
A variable of type Request.Builder<?> can still call timeout(), but the result is a capture type, so chaining a subclass method is impossible and only base methods are usable. That is usually acceptable for code that only sets common fields.
8Conclusion
In this answer, we built a self-typed builder hierarchy: Request.Builder<T extends Builder<T>> returns self() from each setter, and HttpBuilder extends Request.Builder<HttpBuilder> so base and subclass methods chain in any order. We saw its costs, harder signatures, confusing compiler errors, raw-type and wrong-argument mistakes, and generics spreading through every level, and compared it with separate builders and covariant overrides. For a small, closed hierarchy in a shared library the simpler designs usually win, and the recursive pattern is best reserved for deep or open hierarchies.
Open with
The problem is that a base builder method like timeout returns the base builder type, so a caller who calls timeout first can no longer call header on the HTTP builder.
Then, if they want more
I would fix it with a self type: abstract static class Builder<T extends Builder<T>>, where every setter returns self(), and self() is an abstract method returning T. Then HttpBuilder extends Request.Builder<HttpBuilder>, implements self() as return this, and adds header returning HttpBuilder.
Now timeout called on an HttpBuilder returns HttpBuilder, so callers can chain base and subclass methods in any order, and build can be overridden covariantly to return HttpRequest. There is no unchecked cast because each subclass supplies this through self().
But I would be honest about the cost for a shared library: the signatures are harder to read, misuse gives confusing inference errors, raw types or a wrong type argument silently break chaining, and every extensible level has to carry the type parameter.
So if we only have two or three request types, I would prefer separate builders or covariant overrides of the few base setters. I would reserve the recursive pattern for deep or open hierarchies, and hide it behind static builder() factory methods so callers never write the type arguments.
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 "Design a fluent builder hierarchy for a base Request and a subclass HttpRequest using...": https://scrivio-interview-library.netlify.app/java/generics/c3289f366c/