Last active
August 29, 2015 14:25
-
-
Save raphw/005b0a3deb1879309d82 to your computer and use it in GitHub Desktop.
Bridge method resolution.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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