Class Is Not Abstract and Does Not Override Error in Java

Rupam Yadav Dec 21, 2022 Dec 29, 2021
  1. Why Does the Error Class is not abstract and does not override abstract method Occur in Java?
  2. Solution 1: Override the canSpeak() Method
  3. Solution 2: Make Baby Abstract
  4. Solution 3: Make Human a Class and Extend It in the Baby Class
Class Is Not Abstract and Does Not Override Error in Java

This article will find solutions to the error Class is not abstract and does not override abstract method that occurs when we use the concept of abstraction in Java.

Why Does the Error Class is not abstract and does not override abstract method Occur in Java?

In the code below, we have two classes and an interface. The class JavaExample has a main() method without any body part. We create a Human interface with an abstract method canSpeak() with boolean as a return type. We do not specify a body for canSpeak() because an abstract method doesn’t have a body.

In the second class, Baby, we inherit the Human interface using the implements keyword. If we use an IDE, there will be an error, and when we run the code, the error will occur, as shown in the output.

If we look at the error Baby is not abstract and does not override abstract method canSpeak() in Human, we can understand why it occurred. It says that the class Baby is not abstract, and it doesn’t override the method canSpeak() of the Human interface.

This error appears because we have to override the abstract methods to define the body when implementing any class interface with abstract methods.

public class JavaExample {
    public static void main(String[] args) {
    }

}

class Baby implements Human {

}

interface Human {
    abstract boolean canSpeak();
}

Output:

java: Baby is not abstract and does not override abstract method speak() in Human

Solution 1: Override the canSpeak() Method

To fix the Baby is not abstract and does not override abstract method speak() in Human error, the first solution is to override the abstract method canSpeak() in the Baby class that implements the Human interface.

The canSpeak() function returns false and in the main() method we create an object of the Baby class and call the overridden function canSpeak(). In the output, we can see that there’s no error, and the expected value shows.

public class JavaExample {
    public static void main(String[] args) {
        Baby baby = new Baby();
        System.out.println("Can Speak? "+baby.canSpeak());
    }

}

class Baby implements Human {

    @Override
    public boolean canSpeak() {
        return false;
    }
}

interface Human {
    abstract boolean canSpeak();
}

Output:

Can Speak? false

Solution 2: Make Baby Abstract

Another solution involves making the class Baby an abstract. We have the same code here, but Baby is an abstract class. It cannot be initialized; we only create an instance of the Baby class in the main() method.

public class JavaExample {
    public static void main(String[] args) {
        Baby baby;
    }

}

abstract class Baby implements Human {

}

interface Human {
    abstract boolean canSpeak();
}

Solution 3: Make Human a Class and Extend It in the Baby Class

The last solution is a different one. Instead of implementing an interface, we can change the interface Human to a class and extend that class in the Baby class using the keyword extends. We specify the body of canSpeak() in the Human class itself, which removes the error.

public class JavaExample {
    public static void main(String[] args) {
        Baby baby = new Baby();
        System.out.println("Can Speak? " + baby.canSpeak());
    }
}

class Baby extends Human {

}

class Human {
    boolean canSpeak() {
        return false;
    }
}

Output:

Can Speak? false
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java Error

Related Article - Java Class