How to Resolve a java.lang.AbstractMethodError in Java?
The java.lang.AbstractMethodError is a runtime error in Java that occurs when a class lacks the implementation of the abstract method declared in one of its interfaces or abstract parent classes. This error signifies a mismatch between the expected and actual class hierarchy.
Syntax:
public interface MyInterface {
void myMethod();
}
public class MyClass implements MyInterface {
// Missing implementation of myMethod
}
Program to Resolve a java.lang.AbstractMethodError in Java
The java.lang.AbstractMethodError occurs when a class attempts to invoke an abstract method that should have been implemented by the concrete subclass but isn’t.
Below is the Program to Resolve a java.lang.AbstractMethodError:
Java
interface GFG { void myMethod(); } // Class implementing the interface // And providing the implementation class myClass implements GFG { // Implementation of abstract method public void myMethod() { System.out.println( "Implementation of myMethod" ); } public static void main(String[] args) { // Creating an instance of MyClass myClass obj = new myClass(); // Calling the implemented method obj.myMethod(); } } |
Output:
Implementation of myMethod
Explanation of the above Program:
- We define an interface GFG with the abstract method myMethod().
- We create a class myClass that implements the GFG interface. This class provides an implementation for myMethod() method.
- Inside the myMethod() implementation we print the message “Implementation of myMethod”.
- In the main() method we create an object obj of myClass class.
- We then call the myMethod() method on obj object in which invokes the implemented method in myClass class.
- When the program is executed and it prints the message “Implementation of myMethod” to the console.