I get the following error message (reduced to the important part) when I'm compiling my classes:
reference to keySet is ambiguous, both method keySet() in
java.util.SortedMap<E,capture#614 of ?> and method keySet() in
test.ImmutableMap<E,capture#614 of ?> match
return map.keySet().iterator();
^
map
is of type ImmutableSortedMap<E, ?>
and the definition of immutable classes looks like:
public interface ImmutableMap<K, V>
extends Map<K, V> {
@Override
public ImmutableSet<K> keySet();
...
}
public interface ImmutableSortedMap<K, V>
extends ImmutableMap<K, V>, SortedMap<K, V> {
...
}
public interface ImmutableSet<E>
extends Set<E> {
...
}
The error appears when I'm compiling using an ANT script or manually, but not in eclipse. I tried it with sun 1.6.0 and icedtea6 1.8.1.
Is there something obvious I'm missing or is there an option that may be set somewhere in eclipse to allow the compilation? The thing is that, right now, I can run the tests in eclipse, but I cannot compile the project outside eclipse.
EDIT: the answer
It seems that some compiler versions have problems with multiple inheritance like that. The solution is to override the method one more time in the sub-class inheriting from other interfaces that have a common super-interface and that override themselves the method.
public interface ImmutableSortedMap<K, V>
extends ImmutableMap<K, V>, SortedMap<K, V> {
// adding this method solves the problem
@Override
public ImmutableSet<K> keySet();
}
By the way, this is the diamon problem.