Recently I'm testing java generic feature. Here is the testing code:
package test;
public class GenericAndMethodSignature {
public static void main(String[] args) {
(new ClazzAAA()).fooo();
}
public abstract static class ClazzAA<T> {
public final void fooo() {
System.out.println(this.foo((T) null));
}
public abstract String foo(T input);
public final String foo(Integer input) {
return "foo";
}
}
public static class ClazzAAA extends ClazzAA<Integer> {
}
}
If I compile and run it with Eclipse, console will show:
Exception in thread "main" java.lang.AbstractMethodError: test.GenericAndMethodSignature$ClazzAA.foo(Ljava/lang/Object;)Ljava/lang/String;
at test.GenericAndMethodSignature$ClazzAA.fooo(GenericAndMethodSignature.java:12)
at test.GenericAndMethodSignature.main(GenericAndMethodSignature.java:6)
However, if I compile it with javac command:
javac test/GenericAndMethodSignature.java
and run it with command
java test.GenericAndMethodSignature
The terminal will show "foo" successfully.
Also, an interesting thing, if I run the class compiled by eclipse with java command, I will get java.lang.AbstractMethodError too.
I use java byte code editor to check those two class, and find ClazzAAA compiled by javac overrides the generic method while class compiled by eclipse not.
Does anyone know why the behavior of these two compiler is different?
Not sure which result is correct.
javacis correct.