Engineering With Java

Engineering With Java

Senior Java Interview: Bug Fix in a Generic Repository

Understanding wildcard variance in Java generics — and why you can never have both safe reads and safe writes at once

Suraj Mishra's avatar
Suraj Mishra
Aug 20, 2026
∙ Paid

Scenario

Your team has built a generic repository used by several Spring Boot services for caching reference data before it’s persisted.

A teammate recently tried to reuse it for a new feature and hit a compilation error.

You’re reviewing their pull request.

The new feature does this:

Repository<User> repository = new Repository<>();

List<AdminUser> admins = List.of(
        new AdminUser("Alice"),
        new AdminUser("Bob")
);

repository.saveAll(admins);   // Compilation error

📢 Get actionable Java and Spring Boot insights every week, including practical code tips and real-world, use-case-based interview questions, to help you level up your backend skills—join 8300+ subscribers for hand-crafted, no-fluff content.

Upgrade to paid now (60% discount) and get the annual membership at $50/year forever that is ~ $4/mo.

So far we have covered 79+ real world based interview questions and will add up to 100 by end of this year.

Testimonials


Why this code doesn't compile?

A common misconception is:

"AdminUser extends User, so List<AdminUser> should extend List<User>."

That's not how Java generics work. Generics are invariant.

User's avatar

Continue reading this post for free, courtesy of Suraj Mishra.

Or purchase a paid subscription.
© 2026 Suraj Mishra · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture