Re: Strange runtime error: AbstractMethodError
Oliver Wong wrote:
After a bit for trimming, I've formed an SSCCE that doesn't require any
external libraries:
<SSCCE>
interface Root {
public Root someMethod();
}
interface Intermediary extends Root {
public Leaf someMethod();
}
class Leaf implements Intermediary {
@Override
public Leaf someMethod() {
return null;
}
}
public class BugTest {
public static void main(String[] args) {
Leaf leafReference = new Leaf();
leafReference.someMethod();
Root rootReference = leafReference;
rootReference.someMethod(); /* throws error */
}
}
</SSCCE>
<output>
Exception in thread "main" java.lang.AbstractMethodError:
Leaf.someMethod()LRoot;
at BugTest.main(BugTest.java:21)
</output>
But I'm still unsure if this is a bug in the JVM, the compiler, or if this
is the correct behaviour of the Java language, using some rule I'm not
familiar with.
I think we're dealing with
<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8.4>
When I try your example I immediately but not consistently get an error from
Netbeans on the @Override,
"method does not override a method from its superclass"
but javac doesn't show me this, not even with -Xlint:overrides.
Now, try adding these two classes to the source:
interface Othif extends Root
{
public String someMethod(); // String is not a subtype of Root
}
class OthImpl implements Othif
{
@Override
public String someMethod()
{
return null;
}
}
These give the expected compilation errors:
src/testit/BugTest.java:30: someMethod() in testit.Othif clashes with
someMethod() in testit.Root; attempting to use incompatible return type
found : java.lang.String
required: testit.Root
public String someMethod();
src/testit/BugTest.java:33: testit.OthImpl is not abstract and does not
override abstract method someMethod() in testit.Root
class OthImpl implements Othif
src/testit/BugTest.java:36: someMethod() in testit.OthImpl cannot implement
someMethod() in testit.Root; attempting to use incompatible return type
found : java.lang.String
required: testit.Root
public String someMethod()
- Lew