. Advertisement .
..3..
. Advertisement .
..4..
This instruction will guide you thoroughly how the error class is not abstract and does not override abstract method can be addressed. Jump right in for further helpful information!
Why Does The Error Class Is Not Abstract And Does Not Override Abstract Method Happen?
This error occurs most commonly with the cause that a class A implements an interface B, but A does not override all the methods of B, while A is also not an abstract class (If A is abstract then this is permitted).
For example:
package javaapplication3;
public class JavaApplication3 {
public static void main(String[] args) {
}
}
class Dog implements Pet{
}
interface Pet{
abstract boolean roar();
}
In the code above, we can see an interface and two classes. Note that there is no body in the main()
function of the JavaApplication3
class.
Roar()
, an abstract method with boolean as the return type, is then created as part of the Pet interface. The thing is roar()
does not have a body since it is an abstract method, which lacks a body as usual.
As such, by using the implements keyword, we shall inherit the Pet interface in the second class, Dog. If we use an IDE, there will be a mistake, and the output will reveal the problem when we execute the code.
As a result, the below error will appear:
Dog is not abstract and does not override abstract method roar() in Pet
How To Solve The Error Class Is Not Abstract And Does Not Override Abstract Method
Remedi #1: Create “Dog” Abstract
In order to correct the error class is not abstract and does not override abstract method, the first step is to make the class Dog
abstract.
Although the code is the same, you can behold in the code the Dog now is an abstract class. We only construct the Dog’s instance in the main()
function. Therefore, it is impossible for this class to be initialized.
Running the code:
public class JavaApplication3 {
public static void main(String[] args) {
Dog dog;
}
}
abstract class Dog implements Pet {
}
interface Pet {
abstract boolean roar();
Remedi #2: Override The Roar() Function
Another potential approach to come is to override the roar()
function in the Dog class that executes the Pet interface.
The main()
method creates an object of the Dog class and calls the overridden method roar()
. The roar()
function then returns false. As a consequence, we can see from the output that there is no problem and that the anticipated result is displayed.
Running the code:
public class JavaApplication3 {
public static void main(String[] args) {
Dog dog = new Dog();
System.out.println("Roar? "+dog.roar());
}
}
class Dog implements Pet {
@Override
public boolean roar() {
return false;
}
}
interface Pet {
abstract boolean roar();
}
Output:
Roar? false
Remedi #4: Create The Class Of Pet And Extend It To The Dog Class.
The last solution is quite an alternative.
That way, using the term extends, we can convert the interface Pet into a class and extend that class in the Dog class in place of implementing an interface.
The problem is fixed since the Pet class has the canSpeak()
function’s body.
Running the code:
public class JavaApplication3 {
public static void main(String[] args) {
Dog dog = new Dog();
System.out.println("Roar? "+dog.roar());
}
}
class Dog extends Pet {
}
class Pet {
boolean roar() {
return false;
}
}
Output:
Roar? false
The Bottom Line
Our detailed explanation of how to resolve the error class is not abstract and does not override abstract method is all that outlined above. We hope this article may be useful to you in some way. See then!
Leave a comment