AbstractMethodError in Java

AbstractMethodError in Java

Take a closer look at Java's AbstractMethodError and understand what it is and when it may happen. AbstractMethodError is thrown when an application attempts to call an unimplemented abstract method.

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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Top Java Mobile App Development Company in USA

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

Top 10+ Java Development Companies in India & Developers Reviews 2020 – TopDevelopers.co

A thoroughly researched list of top 10 Java development companies in India with ratings & reviews to help find the best java developers around India.

Hire Java Developers Team | Java Developer India - Developers.dev

.DEV is a leading java Developer agency. Our offshore Java Developer Staff offers robust cross-platform java development services at an affordable rate. 1000+ Sucessful Projects, 100+ Happy Customers, 650+ Professionals, 60% Saving on Project Cost

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.