Java Abstract Method Implementation: Rules and Examples
Learn how to declare and implement abstract methods in Java, including rules for concrete subclasses, abstract classes versus interfaces, common compiler errors, and lambda alternatives.

An abstract method declares a method signature without a body. In an abstract class, every concrete subclass must implement it; in an interface, every concrete class that implements the interface must implement it as well. This is a core part of Java's inheritance model, and getting the implementation rules right avoids compilation errors and keeps your class hierarchy consistent. In this article, we'll examine Java abstract method implementation from declaration to runtime behavior, including the differences between abstract classes and interfaces.
What Is an Abstract Method in Java?
An abstract method is a method declaration that has no body. It is marked with the abstract keyword and ends with a semicolon instead of a block. For example:
public abstract class Shape {
public abstract double area();
}The area() method has no implementation. Any non-abstract subclass of Shape must provide an implementation. If a subclass does not implement it, that subclass must also be declared abstract.
Abstract methods are declared in abstract classes and interfaces. They cannot be private, static, final, or synchronized; the native and strictfp modifiers are also prohibited. In an abstract class, an abstract method's access can be public, protected, or package-private. private is not allowed because an abstract method must be implementable by subclasses. In an interface, abstract methods are implicitly public.
Declaring an Abstract Method
The syntax is straightforward:
public abstract class Repository {
public abstract void save(String id, String data);
public abstract String find(String id);
}Notice that there is no method body, just a semicolon. The abstract modifier can appear before or after the access modifier, but the conventional order is public abstract. The method declaration includes the return type, name, and parameter list.
An abstract method cannot be declared in an ordinary concrete class. If you try, the compiler will report an error; the class must be marked abstract if it contains abstract methods.
Implementing an Abstract Method in a Concrete Class
A concrete class that extends an abstract class must implement all inherited abstract methods. The implementation can use the @Override annotation to indicate that the method overrides an abstract declaration:
public class Circle extends Shape {
private final double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
}The implementing method must have the same name and parameter list. Its return type must be the same as or a subtype of the declared return type (covariant return type). It can have a more permissive access modifier (e.g., protected in the abstract class, public in the subclass), but not a more restrictive one.
If a concrete class fails to implement any inherited abstract method, the compiler reports an error: Class is not abstract and does not override abstract method .... This is a common mistake when a class is intended to be concrete but is missing one or more method bodies.
Abstract Methods in Abstract Classes vs Interfaces
Before Java 8, interfaces could only declare abstract methods, and all methods were implicitly public abstract. Since Java 8, interfaces can also have default and static methods with bodies. The abstract methods in interfaces still follow the same rule: any concrete class implementing the interface must provide implementations.
The key differences between abstract classes and interfaces for abstract methods:
| Aspect | Abstract Class | Interface |
|---|---|---|
| Keyword | abstract on method | abstract optional (implicit) |
| Access | public, protected, or package-private | public only |
| Fields | Can have instance fields | Only public static final constants |
| Multiple inheritance | A class can extend only one abstract class | A class can implement multiple interfaces |
| Default methods | Not available | Can have default methods with body |
When a class implements an interface with abstract methods, it must provide implementations for all of them unless the class is abstract. For example:
public interface Drawable {
void draw();
}
public class Canvas implements Drawable {
@Override
public void draw() {
// implementation
}
}Common Compilation Errors and How to Fix Them
The most frequent errors related to abstract method implementation are:
- Missing implementation: A concrete class does not implement an abstract method. Fix by adding the method body or making the class abstract.
- Wrong access modifier: The implementing method is more restrictive than the abstract declaration. For example, an abstract method is
publicand the subclass tries to make itprotected. Fix by widening the access. - Incorrect method declaration: The method name or parameter list does not match, or the return type is not compatible. Fix by matching the abstract declaration's name and parameter list, and using the same or a covariant return type.
- Static or final abstract methods: These are not allowed. Remove
staticorfinalfrom the abstract method declaration.
The compiler messages are usually explicit, so reading the error carefully points to the missing method or the access violation.
Runtime Behavior and Dispatch
Abstract methods are resolved at runtime through dynamic dispatch. When you call a method on an object whose static type is the abstract class, the JVM invokes the implementation in the concrete subclass. This is the foundation of polymorphism.
The cost of a virtual method call is slightly higher than a non-virtual call because the JVM must look up the method in the vtable. However, modern JVMs optimize this with inline caching and JIT compilation. In practice, the overhead is negligible for most applications.
Abstract methods are also involved in object initialization. You cannot instantiate an abstract class directly, but when a concrete subclass is created, the superclass constructor runs before the subclass constructor body and field initializers. If a superclass constructor calls an abstract method, dynamic dispatch invokes the subclass implementation before the subclass has been fully initialized. Fields used by that implementation may still have their default values, so calling abstract or other overridable methods from constructors is generally discouraged.
Design Considerations and Maintainability
Using abstract methods is a way to define a contract for subclasses. It forces subclasses to provide specific behavior while allowing the abstract class to define common logic. A common pattern is the template method pattern, where an abstract class defines a skeleton algorithm and lets subclasses fill in the details.
For example:
public abstract class DataParser {
public final void parse() {
String data = readData();
process(data);
}
protected abstract String readData();
protected abstract void process(String data);
}Here, parse() is a concrete method that calls the abstract methods. Subclasses implement readData() and process() to customize the behavior.
When deciding between an abstract class and an interface, consider whether you need to share state or code. Abstract classes are better for code reuse and stateful fields. Interfaces are better for defining capabilities that multiple unrelated classes can implement.
One maintainability concern is that adding a new abstract method to an abstract class breaks all existing concrete subclasses, forcing them to implement the new method. This can be mitigated by using default methods in interfaces (since Java 8) or by providing a default implementation in the abstract class that throws an exception.
Advanced: Anonymous Classes and Lambdas
If an interface has exactly one abstract method (a functional interface), you can implement it with a lambda expression instead of a separate class. For example:
public interface Greeter {
String greet(String name);
}
Greeter greeter = name -> "Hello, " + name;This is a concise way to implement an abstract method without creating a named class. It works only when the target type is a functional interface: an interface with exactly one abstract method, not counting default or static methods. Abstract classes cannot be lambda targets at all, even if they have only one abstract method; lambdas are restricted to functional interfaces.
Anonymous classes can implement abstract methods from both abstract classes and interfaces, but they are more verbose:
Shape shape = new Shape() {
@Override
public double area() {
return 0;
}
};This is useful when you need a one-off implementation, but it adds boilerplate.
The choice between lambdas, anonymous classes, and named concrete classes depends on how often the implementation is reused and how complex it is. For a one-off, a lambda is clean. For repeated use, a named class is better for maintainability.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.