What is the difference between an abstract class and an interface, and when would you choose each?
Interview preparation resource from Gate Smashers.
An abstract class is a base class that can combine shared state, constructors, implemented methods, and abstract methods. An interface defines a contract or capability that classes can implement, often alongside another base class. Choose an abstract class when related subclasses need common implementation or state. Choose an interface when different types should expose the same behavior, especially when a class needs to adopt multiple contracts. The exact rules depend on the language; for example, Java interfaces can include default, static, and private methods, but they do not provide per-object instance fields or constructors.
// Abstract class with state and shared implementation
abstract class Vehicle {
protected int speed;
protected Vehicle(int initialSpeed) {
this.speed = initialSpeed;
}
public void brake() {
speed = Math.max(0, speed - 10);
}
public abstract void accelerate();
}
// Interface representing a capability
interface Flyable {
default void takeOff() {
System.out.println("Taking off");
}
static void checkWeather() {
System.out.println("Checking weather");
}
}
// Plane inherits shared Vehicle behavior and implements Flyable.
class Plane extends Vehicle implements Flyable {
public Plane(int initialSpeed) {
super(initialSpeed);
}
@Override
public void accelerate() {
speed += 50;
}
}
class Main {
public static void main(String[] args) {
Plane plane = new Plane(0);
plane.accelerate();
plane.takeOff();
Flyable.checkWeather();
}
}Abstract class
An abstract class is an incomplete class intended to be extended. It can contain instance fields, constructors, concrete methods, and abstract methods. Subclasses inherit its shared implementation and can supply the missing behavior.
An abstract class is useful when subclasses belong to the same conceptual family and need common state or a common implementation path. In Java, a class can extend only one class, abstract or concrete.
Interface
An interface defines a contract: a type that implements it agrees to provide its required behavior. It is commonly used to model a capability or role that can apply to otherwise unrelated classes.
In Java, interfaces can declare abstract methods and can also contain default, static, and private methods. Default methods can provide reusable behavior, but an interface does not have constructors or per-instance fields.
When to choose each
Use an abstract class when derived classes must share state, initialization through a constructor, protected helper methods, or substantial common implementation. It is also appropriate when the base class defines part of an algorithm and subclasses fill in selected steps.
Use an interface when the main goal is to define an API or capability that many types can adopt. A class can implement multiple interfaces in Java, so interfaces are appropriate when a type must satisfy several independent contracts.
- Abstract class: Choose it for a shared base implementation and state among closely related subclasses.
- Interface: Choose it for a role, capability, or contract that may be implemented by unrelated classes.
Design considerations
Adding or changing behavior in a base class can affect all subclasses, so a base hierarchy should remain focused and stable. Interfaces reduce coupling to a particular implementation, although default methods can still create conflicts when a class inherits incompatible defaults; the class must resolve such conflicts explicitly.
These distinctions are language-specific in detail. The comparison and example below use Java rules.
