What is a bridge method?

What is a bridge method?
In Java, there is no covariance for overridden methods on their parameters – the parameter types must match exactly with those of the parent class method. When a generic parameter is specialized in a subclass, the methods with arguments of that generic type no longer match in bytecode - the subclass has a concrete type, while the parent class has the erased type up to the upper bound.

This issue is resolved by a simple and safe cast. The compiler generates a new method that matches the signature of the parent method. Inside this method, the parameter is downcasted, and the call is delegated to the user-defined method. This is known as a bridge method.

A bridge method can be observed using reflection. Its name matches the original method, but the parameter will be of the type to which the parent's generic has been erased. This method is marked with the synthetic flag, meaning it was generated by the compiler and not written by the programmer.

Attempting to manually implement such a method would result in a compilation error.