What is the difference between an abstract class and an interface?
An abstract class is a partly finished class that can have both regular methods with code and abstract methods without code, and it can hold state through fields. An interface is a pure contract that lists methods a class must provide. A class can extend only one abstract class but can implement many interfaces. Use an abstract class for shared base behaviour and an interface for a capability many unrelated classes can share.
Abstract class: a shared base
An abstract class is meant to be a common parent. It can provide some finished methods and leave others for children to complete. You cannot create an object of an abstract class directly.
abstract class Shape {
abstract double area(); // no body, children must fill in
void describe() { // shared, finished method
System.out.println("I am a shape");
}
}
Interface: a pure contract
An interface says what a class must be able to do, without saying how. Many unrelated classes can implement the same interface to promise the same capability.
interface Drawable {
void draw(); // any Drawable must provide draw()
}
class Circle implements Drawable {
public void draw() { System.out.println("circle"); }
}
Quick comparison
- Methods: an abstract class can have finished and unfinished methods, an interface is traditionally only a contract.
- State: an abstract class can hold fields and state, an interface mainly holds constants.
- Inheritance: a class extends one abstract class but can implement many interfaces.
- Use: abstract class for a shared base type, interface for a capability shared across unrelated types.
A strong line is to say use an abstract class when classes share a common base and some code, and use an interface when unrelated classes need to promise the same capability. Mentioning that a class can implement many interfaces but extend only one class shows you know the practical limit.
Common follow up questions
Related interview questions
Want the full OOP guide?
Read every OOP concept with notes, diagrams, and code in one place. Track your progress as you go.
Open the OOP guide All OOP questions