Class is Not Abstract & Does Not Override Abstract Method - Fixed

Class is Not Abstract & Does Not Override Abstract Method - Fixed

posted 7 min read

The error "class is not abstract and does not override abstract method" happens when you forget to inherit all the abstract methods to the concert class that invoked the error. You can resolve the error by overriding and inheriting all the abstract methods in your concrete class.
In this article, we will cover this error by explaining its nature and reason of occurrence with a step-by-step guide to reproduce and resolve it and introducing you to an alternate way of solving the error. Let's get started!

Understanding the Error

Before shifting towards the technical details of this error, you can understand its meaning by splitting the statement, since it occurred on compile time. While using Java abstraction, many beginner or intermediate-level developers encounter this type of error. The abstract class and methods are fundamental elements of Java programming language that are not instantiated on their own and define the methods without implementation. However, frequent mistakes in subclasses generate an error.

In this error,
  • Class is not abstract: indicates that this class extends the abstract class. However, as a non-abstract sub-class, you did not implement the methods of the abstract class, prompting the compiler to expect it as an abstract class.
  • Does not override the abstract method: This piece of error hints you that the non-abstract subclass should override the abstract methods of the superclass, as the compiler expects that the abstract method should override.

Steps to Reproduce the Error

Firstly, to know the solution to this error, you have to reproduce it by invoking the error with a simple example of "Account".

Step 1: Get Started: Open Code Editor

Before demonstrating the error, you need to open your favorite code editor. I prefer to use OnlineGDB for Java compilation.

Step 2: Write Code: Copy and Paste Code Snippets

Let's code in the code editor step by step.

Create an Abstract Class
 
 
//Define an abstract class Account 
public abstract class Account{ 
    //Create a single abstract method of name account details 
   public abstract void accountDetails(); 
} 
 
Create Non-Abstract Class
 
 
//create a non-abstract class that extends the abstract class 
class savingAccount extends Account{ 
    // missing implementations of abstract methods 
} 
 
Create an Instance of Class in Main Class
 
 

public class Main 
{ 
    public static void main(String[] args){ 
        //create an object of a non-abstract class that will invoke an error 
        savingAccount sAccount = new savingAccount();  
        sAccount.accountDetails(); 
    } 
} 
 
Note: Check that the abstract class has a keyword abstract.
Tip: Remember, if you are working in OnlineGDB then you have to create each class file separately in the same program folder.

Step 3: Code Compilation: Compile the Code of Step 2

After that, you just need to compile the code to see the error by clicking the Run button.

Step 4: Compile Time Error Invoke: Output

During the code compilation, you will see that the error is thrown after compilation.

image

Why the Error Occurred?

  • As you can see, in Java the abstract class is defined as a structure or a blueprint of another class. So, when you declare an abstract class, you cannot implement all the abstract methods in it. Also, you cannot create objects and instances for the abstract class. For this reason, the subclass must extend the abstract class and override its methods if the subclass is not defined as an abstract class (subclass). But if the non-abstract class does not override the method of the abstract class at that time it will create an error because you can only call the method of the abstract class i.e. superclass by creating the instance of its subclass.
  • In the above code snippets, we have two classes: one is the abstract class Account and the second is its non-abstract subclass savingAccount. The Account class has one abstract method but when the savingAccount class extends the Account class the abstract method is not overridden in the subclass which causes the error. So, for this reason, we have a quick solution by overriding the accountDetails() method in the subclass savingAccount.

How to Resolve the Error?

If you look at the error acutely, it is easy for you to identify its solution. It clearly states that the savingAccount class is a non-abstract class that extends the abstract class, so it should override the abstract methods as well. Therefore, resolving this error is quite easy and simple by just overriding the accountDetails() method in a subclass.

 
 
public abstract class Account{ // create an abstract class  
   public abstract void accountDetails(); // define an abstract method without implementation 
} 

class savingAccount extends Account{ 
    @Override //use an override annotation before implementing the abstract method 
    public void accountDetails(){ 
        System.out.println("Print All Details"); // implementing the method 
    } 
} 
public class Main 
{ 
    public static void main(String[] args){ 
        //create an object of a non-abstract class that calls the abstract method 
        savingAccount sAccount = new savingAccount();  
        sAccount.accountDetails(); 
    } 
} 
 
Caution: Verify that the abstract method in the sub-class must have the override annotation before its implementation.

After the compilation of the above code, the results will be error-free. Easy and quick solution? Perfect!

image

Another Way of Solving the Error

Further, you can also solve the error by declaring the savingAccount class as an abstract class, but for that, you should have another subclass that should be non-abstract and extend the savingAccount class for creating the instances so you can call the methods of an abstract class. The abstract subclass "savingAccount" is meant to be a base class for other non-abstract classes because you cannot create the objects directly.

Code

 
 
public abstract class Account{ // create an abstract class   
   public abstract void accountDetails(); // define an abstract method without implementation  
}  

abstract class savingAccount extends Account{  
    public abstract void totalAmount(); //create another abstract method of subclass 
} 

class otherAccount extends savingAccount{  
    @Override // override the abstract method of the superclass Account 
    public void accountDetails() 
    { 
         System.out.println("Print All account details"); 
    } 
    @Override //override the abstract method of  savingAccount class 
     public  void totalAmount(){ 
         System.out.println("The Total Amount is Rs. 3000"); 
     } 
} 

public class Main  
{  
    public static void main(String[] args){  
        //create an object of a non-abstract class that calls the abstract method  
        otherAccount oa = new otherAccount();   
        oa.accountDetails(); // call the method of super class Account 
        oa.totalAmount(); //call the method of savingAccount class that is the subclass of Account 
    }  
} 
 
Note: If you have more than two abstract classes, you follow the same process by inheriting their methods in the non-abstract class.

In the above code, I created a non-abstract class name as otherAccount or the savingAccount and the Account as an abstract class. The otherAccount class extends the savingAccount class and inherits its abstract function for implementation. This class also overrides the abstract function of the Account class. After overriding all the abstract methods of superclasses, the object is created with the name "oa" and calls the methods of both abstract classes. In the end, compile the code to see the output.

Output
image

FAQs Q: What is the significance of Override annotation?
A: The method having the keyword Override specifies that this method is inherited and implemented from the superclass.
Q: What are abstract classes in Java?
A: Abstract is like a blueprint, a class that does not have implemented abstract methods only methods have a definition. These classes cannot create instances on their own. However, the class should have a keyword abstract.
Q: Can an error occur if multiple abstract classes are implemented?
A: Yes, if there are two or more abstract classes and these classes are inherited by the concrete class, but the non-abstract class did not implement all the abstract methods, then it surely throws the error.

Conclusion

In this article, we talked about the "Class is Not Abstract and Does Not Override Abstract Method" error in Java. We saw examples with the help of coding that raised this error and learned different ways to fix it. However, if the developer understands the concept of "Java Abstraction" then they will effortlessly resolve this issue and will not run into it again in the future. As you know, the Java compiler helps the developer by ensuring to stick with the strong type checking and strictly follows all the fundamentals of the Java programming paradigm. So, whenever you use abstract classes and methods, the foremost you should do is to override all the abstract methods in the concrete class and then implement the other methods. This approach will make your coding process smooth. I hope this guide helps you to fix the error. Thank you for reading the article. Happy Coding!


Reference

Abstraction in Java

If you read this far, tweet to the author to show them you care. Tweet a Thanks

More Posts

Checked exception is invalid for this method

prince yadav - Nov 26, 2023

Invalid Method Declaration; Return Type Required in Java

Abdul Daim - Feb 22, 2024

[SOLVED] Void type not allowed here

prince yadav - Nov 26, 2023

What is Jit compiler?

stjam - Feb 2

Program does not contain a static 'main' method suitable for an entry point

Rafael Borges Batist - Feb 23, 2024
chevron_left