Side-by-side comparison of Java Abstract Class vs Interface — updated for modern Java (Java 17+).
Abstract Class vs Interface in Java
Feature | Abstract Class | Interface |
|---|---|---|
Keyword | abstract class | interface |
Instantiation | Cannot be instantiated | Cannot be instantiated |
Methods | Can have abstract and concrete methods | Can have abstract, default, and static methods (Java 8+), and private methods (Java 9+) |
Fields | Can have instance variables and constants | Only public static final constants (no instance variables) |
Constructors | ✅ Can have constructors | ❌ Cannot have constructors |
Access Modifiers | Methods can have any access modifier | All abstract methods are implicitly public |
Inheritance | Supports single inheritance | Supports multiple inheritance (a class can implement multiple interfaces) |
State | Can maintain state via instance variables | Cannot maintain state (only constants) |
When to Use | When classes share state and behavior, but also need to enforce certain methods | When you need to define a contract without shared state |
Extends / Implements | A class can extend only one abstract class | A class can implement multiple interfaces |
Performance | Slightly faster (direct inheritance) | May have small overhead due to multiple inheritance resolution |
Quick Example
// Abstract Class Example
abstract class Animal {
String name;
abstract void makeSound();
void eat() {
System.out.println(name + " is eating.");
}
}
// Interface Example
interface Flyable {
void fly();
}
class Bird extends Animal implements Flyable {
Bird(String name) {
this.name = name;
}
public void makeSound() {
System.out.println("Chirp!");
}
public void fly() {
System.out.println(name + " is flying.");
}
}
public class Main {
public static void main(String[] args) {
Bird b = new Bird("Sparrow");
b.makeSound();
b.eat();
b.fly();
}
}💡 Rule of Thumb:
- Use abstract class when you need shared code + enforced methods.
- Use interface when you need a pure contract that multiple unrelated classes can follow.