Difference Between Interface and Abstract Class
Interface vs abstract class in OOP — contract vs partial implementation, multiple vs single inheritance and when to use each — with Java examples and Q&A.
Expected Interview Answer
An interface defines a contract of methods a class must implement with no state, while an abstract class can provide partial implementation and state and serves as a shared base for closely related classes.
A class can implement many interfaces but extend only one abstract class, so interfaces model capabilities ("can do") across unrelated types, while abstract classes model an "is-a" hierarchy with shared code. Abstract classes can hold fields, constructors, and concrete methods; interfaces traditionally hold only method signatures (though modern languages allow default methods). Use an interface for a common capability, an abstract class to share implementation among related classes.
- Interface: multiple inheritance of type / capabilities
- Abstract class: shared code and state for related classes
- Both enable programming to a type, not an implementation
AI Mentor Explanation
An interface is like the "Captain" role — a contract of duties (win the toss call, set the field, rotate bowlers) that any player can take on regardless of whether they bat or bowl. An abstract class is like "Bowler" — a base type sharing real technique and stamina training, extended by fast bowlers and spinners. A player can hold many roles (interfaces) but belongs to one primary type (abstract class). Capability versus shared identity.
Step-by-Step Explanation
Step 1
Interface = contract
Method signatures a class promises to implement; no state (traditionally).
Step 2
Abstract class = partial base
Can hold fields, constructors and concrete methods to share code.
Step 3
Inheritance limits
Implement many interfaces; extend only one abstract class.
Step 4
Choose by intent
"Can-do" capability across types → interface; "is-a" with shared code → abstract class.
What Interviewer Expects
- Contract vs partial implementation as the core distinction
- Multiple interfaces vs single abstract class inheritance
- State/constructors allowed in abstract classes, not interfaces
- When to choose each
Common Mistakes
- Saying interfaces can have constructors or instance state
- Claiming a class can extend multiple abstract classes (in Java)
- Treating them as interchangeable
- Ignoring default methods in modern interfaces
Best Answer (HR Friendly)
“An interface is a contract that lists what a class must do, with no shared code, and a class can implement many. An abstract class is a partial base that shares real code and state among related classes, and a class extends only one. Use an interface for a capability, an abstract class to share implementation.”
Code Example
interface Drawable { // contract only
void draw();
}
abstract class Shape { // shared state + code
protected String color;
Shape(String color) { this.color = color; }
abstract double area();
String describe() { return color + " shape"; }
}
class Circle extends Shape implements Drawable {
double r;
Circle(String c, double r) { super(c); this.r = r; }
double area() { return Math.PI * r * r; }
public void draw() { /* ... */ }
}Follow-up Questions
- Can an abstract class have a constructor? Why?
- What are default methods in interfaces and why were they added?
- Can Java classes inherit from multiple abstract classes?
- When would you prefer an interface over an abstract class?
MCQ Practice
1. How many abstract classes can a Java class extend?
Java supports single class inheritance — one (abstract) superclass — but multiple interfaces.
2. Which can hold instance fields and constructors?
Abstract classes can have state and constructors; traditional interfaces cannot.
3. To model a "can-do" capability across unrelated classes, use a?
Interfaces express capabilities that any type can implement, regardless of hierarchy.
Flash Cards
Interface in one line? — A contract of method signatures with no state; implement many.
Abstract class in one line? — A partial base with shared code and state; extend only one.
Inheritance rule? — Multiple interfaces, single abstract class (in Java).
When to pick each? — Capability across types → interface; is-a with shared code → abstract class.