1. Overview

Sometimes, we may encounter AbstractMethodError at runtime in our application. If we don’t know this error well, it might take a while to determine the cause of the problem.

In this tutorial, we’ll take a closer look at AbstractMethodError. We’ll understand what AbstractMethodError is and when it may happen.

2. Introduction to AbstractMethodError

**AbstractMethodError is thrown when an application attempts to call an unimplemented abstract method. **

We know that if there are unimplemented abstract methods, the compiler will complain first. Therefore, the application won’t get built at all.

We may ask how we can get this error at runtime?

First, let’s have a look at where AbstractMethodError fits into the Java exception hierarchy:

java.lang.Object
|_java.lang.Throwable
  |_java.lang.Error
    |_java.lang.LinkageError
      |_java.lang.IncompatibleClassChangeError
        |_java.lang.AbstractMethodError

As the hierarchy above shows, this error is a subclass of IncompatibleClassChangeError. As its parent class’s name implies, AbstractMethodError is usually thrown when incompatibilities exist between compiled classes or JAR files.

Next, let’s understand how this error can happen.

3. How This Error May Happen

When we build an application, usually we’ll import some libraries to make our work easier.

Let’s say, in our application, we include a baeldung-queue library. The baeldung-queue library is a high-level specification library, which contains only one interface:

public interface BaeldungQueue {
    void enqueue(Object o);
    Object dequeue();
}

Also, to use the BaeldungQueue interface, we import a BaeldungQueue implementation library: good-queue. The good-queue library also has only one class:

public class GoodQueue implements BaeldungQueue {
    @Override
    public void enqueue(Object o) {
       //implementation 
    }

    @Override
    public Object dequeue() {
        //implementation 
    }
}

Now, if both good-queue and baeldung-queue are in the classpath, we may create a BaeldungQueue instance in our application:

public class Application {
    BaeldungQueue queue = new GoodQueue();

    public void someMethod(Object element) {
        queue.enqueue(element);
        // ...
        queue.dequeue();
        // ...
    }
}

So far, so good.

Someday, we’ve learned that baeldung-queue released version 2.0 and that it ships with a new method:

public interface BaeldungQueue {
    void enqueue(Object o);
    Object dequeue();

    int size();
}

We want to use the new size() method in our application. Therefore, we upgrade the baeldung-queue library from 1.0 to 2.0. However, we forget to check if there’s a new version of the good-queue library that implements the BaeldungQueue interface changes.

Therefore, we have good-queue 1.0 and baeldung-queue 2.0 in the classpath.

Further, we start using the new method in our application:

public class Application {
    BaeldungQueue queue = new GoodQueue();

    public void someMethod(Object element) {
        // ...
        int size = queue.size(); //<-- AbstractMethodError will be thrown
        // ...
    }
}

Our code will be compiled without any problem.

However, when the line queue.size() is executed at runtime, an AbstractMethodError will be thrown. This is because the good-queue 1.0 library doesn’t implement the method size() in the BaeldungQueue interface.

#java #programming #developer

AbstractMethodError in Java
1.65 GEEK