Thursday, 16 October 2025

Bridge Methods in Java

In Java, Generics are erased ("type erasure") at compile time. This concept introduces a problem where an abstract super class containing generics <T> will be compiled to abstract super class with "Object".

In which case implementation of this abstract class with specific Generics (say for example <String>) will cause the compiler to create Bridge Methods, in this case the someMethod(Object o) will call someMethod(String o) with an appropriate cast.

Reference [1] and [2] has a much better explanation

You won't find it in the JLS, as it is an implementation detail. But interesting none the less.

References

[1] Medium - Bridge Methods in Java
https://medium.com/@rohitsingh341/bridge-methods-in-java-21a9d06b6b1b
[2] The Java Tutorials - Effects of Type Erasure and Bridge Methods
https://docs.oracle.com/javase/tutorial/java/generics/bridgeMethods.html

No comments:

Post a Comment