What is the difference between an abstract class and an interface in Java?
8 minintermediateabstract-classinterfaceoop
Quick Answer
An abstract class can hold state, constructors, and a mix of abstract and concrete methods, but supports only single inheritance. An interface defines a contract (plus, since Java 8, default/static methods), holds only constants, has no constructors, and a class can implement any number of interfaces.
Detailed Answer
| Abstract class | Interface | |
|---|---|---|
| Fields | any (instance state) | only public static final constants |
| Constructors | yes | no |
| Method bodies | abstract + concrete methods | abstract, default, static, private methods |
| Inheritance | single (extends) | multiple (implements several) |
| Access modifiers on methods | any | implicitly public (unless private) |
| When to use | shared state + partial implementation, "is-a" hierarchy | a capability/contract multiple unrelated classes can fulfill |
abstract class Vehicle {
protected int speed;
abstract void accelerate();
void brake() { speed = 0; } // shared implementation
}
interface Drivable {
void steer(double angle);
default void honk() { System.out.println("Beep!"); } // Java 8+
}
class Car extends Vehicle implements Drivable {
void accelerate() { speed += 10; }
public void steer(double angle) { /* ... */ }
}
Rule of thumb: use an abstract class when subclasses share meaningful state/implementation; use an interface to describe a capability that unrelated classes can opt into (and to get multiple "inheritance" of behavior via default methods).