java.lang.AbstractMethodError – How to resolve Abstract Method Error
In this example we will discuss about AbstractMethodError
. As you may have figured out, this is thrown when the application calls an abstract method. Normally, this error is caught by the compiler, it can only occur at run time if the definition of some class has incompatibly changed since the currently executing method was last compiled.
The AbstractMethodError
extends the IncompatibleClassChangeError
class, which occurs when an incompatible class change has occurred to some class definition.
The AbstractMethodError
exists since JDK 1.0.
The structure of AbstractMethodError
Constructor:
AbstractMethodError()
Creates an instance of theAbstractMethodError
class, setting null as its message.
AbstractMethodError(String s)
Creates an instance of theAbstractMethodError
class, using the specified string as message. The string argument indicates the name of the class that threw the error.
The AbstractMethodError in Java
To make an example of AbstractMethodError
, I will use the javac
compiler from the command-line. This error is thrown when the classes are separately-compiled, and most of IDEs don’t allow this.
Create a java class called AbsClass
with the following source code:
public class AbsClass { public void hello() { System.out.println("Hello! I belong to AbsClass"); } }
To compile this class, execute this on the command-line:
javac AbsClass.java
Now that the first class is compiled, create the second class, called MainClass
, with this source code:
public class MainClass extends AbsClass { public static void main(String[] args) { MainClass obj = new MainClass(); obj.hello(); } }
The output of compiling and running this would be:
>_ javac MainClass.java >_ java MainClass Hello! I belong to AbsClass
Things are actually OK, but what would happen if we change the hello()
method to abstract and then recompile AbsClass
without changing MainClass
? Let’s try it, by changing AbsClass
to this:
public abstract class AbsClass { public abstract void hello(); }
Now, I can recompile this class without any problem, but when I run the MainClass
, I get this:
>_ java MainClass Exception in thread "main" java.lang.AbstractMethodError: MainClass.hello()V at MainClass.main(MainClass.java:6)
More about the AbstractMethodError in Java
AbstractMethodError
is thrown when the programmer calls an abstract method without firstly overriding it. Of course, this would resolve in a compile-time error and the compilation would fail, but it doesn’t always go this way.
AbstractMethodError
would happen if a method of the base class changes to abstract, hence changing the base class to abstract, and all this happens without the proper changes in the child class (i.e. overriding the abstract methods). So, just as in the example above, the programmer calls an abstract, not implemented method (without knowledge of its abstractness), and he is not informed of this error since only the base class gets changed and compiled.
How to deal with AbstractMethodError
What you need to do in this case, is just implement the abstract method of the AbsClass
in the MainClass
. This would solve the problem you have with the AbstractMethodError
.
Download Code
You can download the full source code of this example here : AbstractMethodErrorExample