Say we have class
public class Entry<K, V> {
public Entry(K k,V v) {
mKey = k;
mValue = v;
}
public K getKey() {
return mKey;
}
public V getValue() {
return mValue;
}
...
}
Then we write an extending class like:
public class ExtendedEntry<K extends SomeSpecificKey, V> extends Entry<K, V>
Then, one class more in the hierarchy:
public class ConcreteEntry<Option extends Something, K extends MoreSpecificKey<Option>, V> extends ExtendedEntry<K, V>
MoreSpecificKey
here:
public interface MoreSpecificKey<OptionType extends Something> extends SomeSpecificKey {
public void doSomething(OptionType option);
}
And I use doSomething()
method somewhere in the code of ConcreteEntry
.
Then Eclipse compiler says OK and builds code and shows no warnings.
But javac
responses:
...ConcreteEntry.java:6 types ...ExtendedEntry and ...ExtendedEntry are incompatible: both define getKey() method, but unrelated return types
When I change (removing generic type) MoreSpecificKey
to:
public interface MoreSpecificKey extends SomeSpecificKey {
public <OptionType extends Something> void doSomething(OptionType option);
}
ConcreteEntry
definition now:
public class ConcreteEntry<Option extends Something, K extends MoreSpecificKey, V> extends ExtendedEntry<K, V>
Error in javac
is gone, and everything is compiled OK too.
So, what's the reason Eclipse compiles code like this -- is it any compiler parameter defined or different preferences, or differences in compilers. How to handle (fix) this while developing in Eclipse and be sure that it will compile in javac
.