According to the JLS (Java Language Specification):
The notion of subsignature is designed to express a relationship between two methods whose signatures are not identical, but in which one may override the other. Specifically, it allows a method whose signature does not use generic types to override any generified version of that method.
This code is based on the JLS example:
interface CollectionConverter<U> {
<T> List<T> toList(Collection<T> c);
void fooMethod(Class<?> c);
<E>Comparable<E> method3(E e);
Comparable<U> method4(U u);
}
class Overrider implements CollectionConverter<Integer> {
@Override
public List toList(Collection c) {
return null;
}
@Override
public void fooMethod(Class c) {
}
@Override
public Comparable method3(Object o) {
return null;
}
@Override
// compile error, have to change Object to Integer
public Comparable method4(Object u) {
return null;
}
}
According to the JLS, I understand why the first three methods work well, but I can’t figure out why method4 has this compilation error:
The method method4(Object) of type Overrider must override or implement a supertype method.
The signature of
method4inCollectionConverterisYou declare
Overriderto implementCollectionConverter<Integer>, thereby binding the type parameterUtoInteger. The signature then becomes:You can declare a
method4(Object u)inOverrider, but that method signature does not overridemethod4(Integer u)specified in the interface any more than it would if you weren’t using generics at all.