// signatures of the reset method at //1 and //2 after erasure will be different
// then why don't they overload?
public class Test<T>{
public static void main(String[] args) {
WordBox<String> city = new WordBox<String>("Skogland");
city.reset("waiting"); // error: ambiguous
}
}
class Box <T> {
private T theThing;
public Box( T t) { theThing = t; }
public void reset( T t) { theThing = t; } //1
}
class WordBox< S extends CharSequence > extends Box< String > {
public WordBox( S t) { super(t.toString().toLowerCase()); }
public void reset( S t) { super.reset(t.toString().toLowerCase()); } //2
}
// signatures of the reset method at //1 and //2 after erasure will be
Share
java.lang.StringextendsCharSequence, so callingcity.reset("waiting")matches bothWordBox.reset(S extends CharSequence)andBox.reset(String).To fix the problem, you should either ensure
WordBox.reset()accepts the same type asBox.reset(), in which caseWordBox.reset()overridesBox.reset(), or, conversely, make sureWordBox.reset()accepts a type that does not overlap withWord.reset(), in which caseWordBox.reset()overloadsBox.reset(). In the example you give, I get the feeling you’d probably wantWordBox.reset()to overrideBox.reset().