Aldo Ziflaj

About Aldo Ziflaj

Aldo is a student of Computer Engineering and a programming addict. He spares his free time coding, whether mobile, web, or desktop programming. He is also a developer for Mozilla Albania team.

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 the AbstractMethodError class, setting null as its message.
     
  • AbstractMethodError(String s)
     
    Creates an instance of the AbstractMethodError 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

Download
You can download the full source code of this example here : AbstractMethodErrorExample

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close