Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

Given class Name implements Comparable<Name> { public int compareTo(Name o) {...} }, what bridge method does javac generate and why? How can bridge methods appear in stack traces or reflection?

After erasure, Comparable's method is compareTo(Object), but Name only declares compareTo(Name), which would not override it. So javac generates a synthetic bridge method, public int compareTo(Object o), that casts o to Name and calls compareTo(Name), keeping overriding working. The bridge shows up in stack traces when a raw Comparable is given the wrong type and the cast fails, and in reflection, where getDeclaredMethods lists both methods and Method.isBridge() and isSynthetic() identify the generated one; javac also creates bridges for covariant return types.

Why this question is asked

Bridge methods are code we never wrote that still runs in every application. Most of the time they are invisible, but they explain a strange stack trace line with no matching source, a framework that sees a method twice, or a ClassCastException thrown from a line that seems to contain only a method signature. Interviewers ask about them to see whether we understand how erasure interacts with overriding. In this answer, we will recap the problem erasure creates for overriding, look at the bridge javac generates for Name, watch it fail in a stack trace, find it with reflection, and then see the other case where bridges appear: covariant return types.

1The Problem Erasure Creates for Overriding

Overriding in the JVM is decided by exact method descriptors: name, parameter types and return type. When a call is made through an interface, the JVM looks for a method in the object's class with exactly the descriptor the caller used.

Comparable<T> declares int compareTo(T o). After erasure, T becomes Object, so the interface method in bytecode is compareTo(Ljava/lang/Object;)I. Our class Name declares compareTo(Name), whose descriptor is compareTo(LName;)I. To the JVM these are two different methods. Code such as Collections.sort, which calls compareTo through Comparable, would look for compareTo(Object) on a Name object and not find our method. Something has to connect the two.

2The Bridge javac Generates

javac solves it by adding a method to Name that we can see with javap. It has the erased interface descriptor, it is marked as synthetic and as a bridge, and its body simply casts and forwards.

Java
class Name implements Comparable<Name> {
final String value;
Name(String value) { this.value = value; }
public int compareTo(Name o) { return value.compareTo(o.value); }
// generated by javac, shown here as Java source:
// public synthetic bridge int compareTo(Object o) {
// return compareTo((Name) o); // checkcast Name, then invokevirtual compareTo(Name)
// }
}
// javap -p Name.class
// public int compareTo(Name);
// public int compareTo(java.lang.Object); // ACC_BRIDGE, ACC_SYNTHETIC
Step throughA call through Comparable reaches the real method
Collections.sortName bridge compareTo(Object)Name compareTo(Name)
compareTo(Object) via Comparable
checkcast Name
compareTo((Name) o)
-1, 0 or 1
result

5 messages in order. Play them, or step through them one at a time.

So when Collections.sort calls compareTo(Object), the JVM finds the bridge, the bridge casts the argument to Name, and our real method runs. Overriding now works exactly as the Java source suggested, even though the generic type was erased. The cast inside the bridge is the same kind of compiler-inserted cast that keeps all erased code safe.

3How a Bridge Appears in a Stack Trace

The cast in the bridge can fail. Generic code is safe as long as everyone respects the types, but raw types switch off the compiler's checks. If someone holds a Name as a raw Comparable and passes it a String, the call goes to the bridge, and the bridge's cast throws.

Java
Comparable raw = new Name("ann"); // raw type: compiler warning only
raw.compareTo("bob"); // ClassCastException
// Exception in thread "main" java.lang.ClassCastException:
// class java.lang.String cannot be cast to class Name
// at Name.compareTo(Name.java:1)
// at Main.main(Main.java:6)

Notice the frame Name.compareTo(Name.java:1). The bridge has no source of its own, so javac gives it the line number of the class declaration, which is line 1 here. Developers often stare at that line looking for a cast that is not there. The real cause is the caller that used a raw type, or a heterogeneous collection, such as a TreeSet that was given both a Name and a String through a raw reference.

4Finding Bridges With Reflection

Reflection sees the class as the JVM does, so it lists the bridge as an ordinary public method. This surprises code that scans methods, for example a framework looking for handler methods.

Java
for (Method m : Name.class.getDeclaredMethods()) {
System.out.println(m + " bridge=" + m.isBridge() + " synthetic=" + m.isSynthetic());
}
// public int Name.compareTo(Name) bridge=false synthetic=false
// public int Name.compareTo(java.lang.Object) bridge=true synthetic=true

Method.isBridge() is true only for bridge methods, and isSynthetic() is true for any compiler-generated member, which includes bridges and also things like lambda bodies. When we scan methods and want only the ones a person wrote, filtering out bridges with isBridge() is the precise check. Spring's BridgeMethodResolver goes one step further and finds the real method a bridge forwards to, which is useful when we start from the bridge, for example because a call through an interface handed it to us.

5Bridges for Covariant Return Types

Bridges are not only about generics. Since Java 5, an overriding method may return a subtype of the original return type, which is called a covariant return. The JVM, again, matches on the exact descriptor, including the return type, so javac adds a bridge with the old return type.

Java
class Animal { Animal copy() { return new Animal(); } }
class Dog extends Animal {
@Override Dog copy() { return new Dog(); }
// generated: synthetic bridge Animal copy() { return this.copy(); } -> calls Dog copy()
}
Animal a = new Dog();
a.copy().getClass(); // class Dog: the call to Animal copy() hit the bridge

So a Dog class has two copy methods in bytecode, one returning Dog and one returning Animal, and reflection shows both. The same happens when a generic interface method is implemented with a more specific return type, so a single class can end up with several bridges.

6Follow-up questions

Can we call a bridge method directly from Java source?

Not by name in a way that differs from the real method, because the source only sees compareTo(Name) and the inherited compareTo(T). Calling through a raw Comparable reaches the bridge, and so does reflection with getMethod("compareTo", Object.class). In normal typed code, the compiler calls the specific method directly.

Do bridges cost anything at run time?

Very little. A bridge is a cast and a call, and the JIT compiler inlines it in hot code. The cost is mostly in understanding: extra frames in stack traces and extra entries in reflection.

Why would a framework see annotations on a bridge?

Recent javac versions copy method annotations onto bridge methods, so a scanning framework sees @Handler on both the real method and the bridge. If it does not filter with isBridge(), it registers the handler twice.

7Conclusion

In this answer, we saw that erasure turns Comparable<Name>.compareTo(T) into compareTo(Object), which our compareTo(Name) does not override at the bytecode level. javac therefore generates a synthetic bridge compareTo(Object) that casts to Name and forwards the call, keeping overriding working. The bridge's cast is where a ClassCastException comes from when raw types smuggle in a wrong argument, which is why the stack trace points at the class declaration line. Reflection lists both methods, with Method.isBridge() and isSynthetic() marking the bridge, and covariant return types produce bridges too.

What to say in the interviewAbout 70 seconds aloud

Open with

Comparable<Name> erases to an interface with compareTo(Object), and the JVM overrides by exact descriptor, so my compareTo(Name) would not actually override it. To fix that, javac generates a synthetic bridge method in Name: public int compareTo(Object o), which casts o to Name and calls compareTo(Name).

Then, if they want more

Collections.sort calls through Comparable, hits the bridge, and the bridge forwards to my real method, so overriding works as the source says. The bridge shows up in two places.

In stack traces, if someone uses a raw Comparable and passes a String, the cast in the bridge throws ClassCastException, and the frame shows Name.compareTo at the class declaration line, because the bridge has no source line of its own.

In reflection, getDeclaredMethods returns both compareTo methods; isBridge and isSynthetic are true for the generated one, so frameworks that scan methods should filter on isBridge, or use Spring's BridgeMethodResolver to get to the real method.

Bridges are also generated for covariant return types: if Dog overrides Animal copy() with Dog copy(), javac adds a bridge returning Animal that calls the Dog version.

About this answer

Last updated
12 November 2026
Versions it names
Java 5
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 "Given class Name implements Comparable<Name> { public int compareTo(Name o) {...} }...": https://scrivio-interview-library.netlify.app/java/generics/582ee1b3d5/