Skip to content

Instantly share code, notes, and snippets.

@raphw
Last active August 29, 2015 14:25
Show Gist options
  • Save raphw/005b0a3deb1879309d82 to your computer and use it in GitHub Desktop.
Save raphw/005b0a3deb1879309d82 to your computer and use it in GitHub Desktop.
Bridge method resolution.
public abstract class Foo<T> {
abstract void foo(T t);
abstract T bar();
public static void main(String[] args) {
Foo<?> quxFoo = new Qux();
Bar<?> quxBar = new Qux();
Foo<?> barFoo = new Baz();
Bar<?> barBaz = new Baz();
quxFoo.foo(null);
quxBar.foo(null); // does not bridge for foo(Number)
barFoo.foo(null);
barBaz.foo(null);
quxFoo.bar();
quxBar.bar();
barFoo.bar();
barBaz.bar();
}
}
class Bar<S extends Number> extends Foo<S> {
@Override
void foo(S o) {
System.out.println("bar");
}
@Override
S bar() {
System.out.println("bar");
return null;
}
}
class Qux extends Bar {
@Override // parameter override considers erasure.
void foo(Object o) {
System.out.println("qux");
}
@Override
Number bar() { // return type override considers upper bound.
System.out.println("qux");
return null;
}
}
class Baz extends Bar<Integer> {
@Override
void foo(Integer o) {
System.out.println("baz");
}
@Override
Integer bar() {
System.out.println("baz");
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment