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.
- An abstract class can have finished and unfinished methods and can hold state through fields.
- An interface is a contract that lists methods a class must provide.
- A class extends only one abstract class but can implement many interfaces.
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.
class Shape { // abstract base class
public:
virtual double area() = 0; // no body, children must fill in
void describe() { // shared, finished method
cout << "I am a shape";
}
virtual ~Shape() = default;
};abstract class Shape {
abstract double area(); // no body, children must fill in
void describe() { // shared, finished method
System.out.println("I am a shape");
}
}from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self): # no body, children must fill in
...
def describe(self): # shared, finished method
print("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.
class Drawable { // interface via pure virtual
public:
virtual void draw() = 0; // any Drawable must provide draw()
virtual ~Drawable() = default;
};
class Circle : public Drawable {
public:
void draw() override { cout << "circle"; }
};interface Drawable {
void draw(); // any Drawable must provide draw()
}
class Circle implements Drawable {
public void draw() { System.out.println("circle"); }
}from abc import ABC, abstractmethod
class Drawable(ABC):
@abstractmethod
def draw(self): # any Drawable must provide draw()
...
class Circle(Drawable):
def draw(self):
print("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.
Frequently asked questions
When would you pick an interface over an abstract class?
Use an interface when unrelated classes need to promise the same capability, and an abstract class when related classes share a common base and some code.
Can an abstract class have a constructor?
Yes. It cannot be instantiated directly, but its constructor runs when a child object is created to initialise the inherited fields.
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