Java Covariant Return Types and Bridge Methods Explained
Covariant return types in Java: how an override can narrow the return type, what bridge methods the compiler generates and the stack trace effects.

Since Java 5 an overriding method may return a narrower type than the method it overrides. The feature removes casts from client code, and it needs some compiler work that is visible in the bytecode and occasionally in stack traces and reflection output. This explainer covers the rule, the synthetic bridge method the compiler generates to make it work on the JVM, how generics create the same kind of bridge and what the effects are when you read traces, scan classes by reflection or design a public API. Examples compile on JDK 25.
Covariant returns
class Shape {
Shape copy() { return new Shape(); }
}
class Circle extends Shape {
@Override
Circle copy() { return new Circle(); } // narrower return type
}
Circle c = new Circle().copy(); // no cast neededCircle.copy() returns Circle, a subtype of Shape, so the override is legal. A caller holding a Circle gets a Circle back and needs no cast. A caller holding a Shape still sees Shape as the static type, and the call dispatches to the right implementation at run time, as the rules in overloading versus overriding describe. Object.clone() is the best-known case, since subclasses commonly override it to return their own type. The rule applies to reference types only: a method returning int cannot be overridden by one returning long, because primitives are not related by subtyping.
Why the JVM needs a bridge
The JVM identifies a method by its name and its full descriptor, which includes the return type. To the JVM, copy()Lcom/Shape; and copy()Lcom/Circle; are two unrelated methods. If the compiler emitted only the narrower one, code compiled against Shape.copy() would still look for the old descriptor, find the version in Shape, and never reach the override. So the compiler adds a synthetic method to Circle.
// generated, conceptually
Shape copy() { return this.copy(); /* calls Circle copy() */ }Callers compiled against Shape.copy() reach Circle.copy() through this bridge. The bridge exists only in the class file. It never appears in source, and nothing you write has to know about it, until a tool lists methods.
Seeing it
javap -p Circle.classclass Circle extends Shape {
Circle copy();
Shape copy(); // synthetic bridge, flagged ACC_BRIDGE
}javap shows two methods with the same name and different return types, which is illegal in source and fine in bytecode. The bridge is flagged ACC_BRIDGE | ACC_SYNTHETIC. That pair of flags is how every tool tells generated methods from written ones.
Generics create bridges too
Erasure is the second source of bridges, and the more dangerous one.
interface Box<T> { void put(T value); }
class StringBox implements Box<String> {
public void put(String value) { }
}After erasure, Box.put takes Object. The compiler adds put(Object) to StringBox, which casts its argument to String and calls put(String). A raw call with the wrong type fails inside the bridge, not at the call site.
Box raw = new StringBox();
raw.put(42); // ClassCastException in the bridge methodThe exception's top frame is the synthetic bridge, and the line number points at the class declaration, which confuses people reading the trace. This is another way to produce heap pollution: the type checks were skipped at the call site with a raw type, and the bridge is where the missing check finally runs.
Effects you may see
Stack traces include the bridge frame, usually marked as a synthetic line, so there is one extra frame between the caller and the real method. When you read a trace that shows the same method name twice in a row, the first is probably the bridge.
Reflection returns both methods from getDeclaredMethods(). A framework that scans for annotated methods could find the same method twice, unless it checks isBridge(). Since Java 8 the compiler copies annotations onto the bridge to reduce that problem, but code that counts methods or registers handlers should still skip bridges.
for (Method m : Circle.class.getDeclaredMethods()) {
if (!m.isBridge()) {
System.out.println(m);
}
}Mocking libraries and serialisation frameworks can also be confused by two methods with the same name. If a mocking error mentions a method defined twice, look for a bridge.
Design guidance
Use covariant returns for fluent copy methods, builders and factory methods in class hierarchies, where they save the caller a cast and keep the chain typed. The self-typed builder is the common example: each subclass's build() returns its own type. Be careful in public APIs you expect third parties to extend. Narrowing a return type in a later release changes the method descriptor, and a binary compiled against the old release can fail with NoSuchMethodError until it is recompiled, even though the source still compiles. Treat a return type change as a compatibility event.
A worked example: the self-typed builder
The most useful application is a builder hierarchy where each level returns its own type.
abstract class Request<T extends Request<T>> {
String url;
abstract T self();
T url(String url) { this.url = url; return self(); }
}
class PostRequest extends Request<PostRequest> {
String body;
@Override PostRequest self() { return this; }
PostRequest body(String body) { this.body = body; return this; }
}
PostRequest r = new PostRequest().url("/orders").body("{}");The call url(...) is declared in the parent and returns the subclass type, which is what lets the chain continue with body(...). Here covariance comes from a generic self type rather than from overriding, and it produces the same absence of casts. The cost is a more complex class declaration, so use it where a fluent API in a hierarchy is the goal, and plain covariant overrides where a single method needs a narrower type.
Checking for bridges in your own code
If a library you maintain changes a return type, run javap -p on the old and new class files and compare the lists. A new bridge in the output means the change affects binary compatibility, even though source code still compiles. Tools such as japicmp do this comparison in the build and fail the release when a public method disappears or changes its descriptor, which is the practical safeguard.
Interfaces and records
Covariant returns work across interface implementations too. A Supplier<Shape> implementation may declare Circle get(), and callers holding the implementing class avoid the cast. Records implement interfaces the same way, with accessor methods that may narrow the return type of an interface method of the same name.
Covariant returns and mocking
Mocking frameworks generate subclasses and therefore bridge methods too, so a mock of a class with covariant overrides works through the bridge. If a stubbed call seems to be ignored, check that the stub targets the same method signature the code calls: stubbing the narrow version while the code calls through the wide parent type is a rare cause of confusing failures. Calling through the declared type used in production avoids it.
Where covariance does not apply
Static methods are hidden, not overridden, so their return types are not covariant. Parameters are not covariant either: a method with a narrower parameter type is an overload, which brings back the compile-time selection rules. The rules are in JLS 8.4.5, and the generics tutorial has a section on bridge methods. Generics in depth are in Java generics explained.


