Abstract Classes vs Interfaces in Java
Introduction
In Java, both abstract classes and interfaces allow you to define contracts and shared behavior for other classes. But when should you use one over the other?
This article explores the differences, use cases, and practical examples of abstract classes and interfaces to help you choose the right approach for your design.
1. What is an Interface?
An interface defines a contract. It contains method signatures that implementing classes must provide.
interface Animal {
void eat();
void sleep();
}
A class implements an interface like this:
class Dog implements Animal {
public void eat() {
System.out.println("Dog eats.");
}
public void sleep() {
System.out.println("Dog sleeps.");
}
}
Since Java 8, interfaces can also contain:
default
methods with implementationstatic
methodsprivate
methods (from Java 9+)
2. What is an Abstract Class?
An abstract class is a class that cannot be instantiated and may contain:
- Abstract methods (no body)
- Concrete methods (with body)
- Fields and constructors
abstract class Vehicle {
String brand;
abstract void start();
void fuelUp() {
System.out.println("Filling fuel");
}
}
A subclass must provide implementations for all abstract methods:
class Car extends Vehicle {
void start() {
System.out.println("Car is starting...");
}
}
3. Key Differences
Feature | Interface | Abstract Class |
---|---|---|
Inheritance type | Implements | Extends |
Multiple inheritance | Yes (can implement many interfaces) | No (can extend only one class) |
Constructors | Not allowed | Allowed |
Fields | public static final only |
Any access modifier |
Method body allowed | default , static (Java 8+) |
Yes (concrete + abstract methods) |
Use case | Capability or contract | Shared base behavior + state |
4. When to Use What
-
Use interfaces when:
- You want to define a capability or contract (e.g.,
Comparable
,Runnable
) - You need to support multiple inheritance
- You want to define a capability or contract (e.g.,
-
Use abstract classes when:
- You want to share common state or logic
- You want to provide a partially implemented blueprint
5. Real-World Example: Notification System
Let’s say you want to define a framework for sending notifications.
Option 1: Interface
interface Notifier {
void send(String message);
}
Used when each implementation is completely independent (Email, SMS, Slack...).
Option 2: Abstract class
abstract class BaseNotifier {
void log(String message) {
System.out.println("LOG: " + message);
}
abstract void send(String message);
}
Used when you want to enforce logging or setup logic in all notifiers.
Conclusion
Both abstract classes and interfaces are powerful tools in Java. Use them deliberately:
- Interfaces = “what a class can do”
- Abstract classes = “what a class is and shares”
Understanding the distinction makes your code more expressive, reusable, and aligned with object-oriented best practices.
You can find the complete code of this article here in GitHub.
📚 Related: Polymorphism and Method Overriding in Java