LearnThatStack Ace your next interview
Object-Oriented Programming · question
Question 7 of 90

How does an abstract class differ from an interface?

beginner
← All Object-Oriented Programming questions
Re-explain

An interface states a contract and nothing else. An abstract class states a contract plus part of the implementation. It can hold fields, constructors and finished methods that subclasses share.

  • A class can implement many interfaces but extend only one abstract class. That makes an interface cheap to add to a type that already has a parent.
  • An abstract class can carry state and enforce an order of steps.
  • Classes from unrelated hierarchies can implement the same interface.

Some languages now let interfaces carry default method bodies. State and constructors still belong to classes. The choice comes down to whether subclasses share code, or only share a capability that callers depend on.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Object-Oriented Programming One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • Should a new class hierarchy start from an interface or an abstract class? Start from the interface callers depend on. Add an abstract base under it only when implementations really share code or state.
  • Why do most languages allow many interfaces but only one base class? Two base classes can bring clashing fields and constructor order. A contract with no state has little to clash, so a class can stack many.
  • If interfaces can have default methods, what can an abstract class still do that an interface can't? Name instance fields and constructors. A default method has no per-object fields of its own to read or write.
  • Two interfaces give one class the same default method. What happens, and how is it resolved? Name the diamond problem. The usual rule is that the class must override the clashing method and can call one parent's version.
  • How would an abstract class make subclasses run steps in a fixed order? Name the template method pattern. A non-overridable method calls abstract hooks in a set order, so subclasses fill in steps but can't reorder them.

What you can say

  1. An interface is only a contract, while an abstract class is a contract plus part of the implementation.
  2. So an abstract class can hold fields, constructors and finished methods that every subclass shares, and it can enforce an order of steps.
  3. A class can implement many interfaces but extend only one abstract class, so an interface is cheap to add to a type that already has a parent.
  4. That also means classes from unrelated hierarchies can implement the same interface.
  5. Some languages now let interfaces carry default method bodies, but state and constructors still belong to classes.
  6. My rule is that shared code points to an abstract class, but if subclasses only share a capability callers depend on, I use an interface and keep the one parent slot free.

Weak answers to avoid

  • Says interfaces can never have method bodies Many languages now allow default methods, so this sounds dated. Say interfaces can carry defaults but still hold no state or constructors.
  • Recites keywords instead of the design choice Modifier rules are trivia. Say when you'd pick each one, shared code versus a capability callers depend on, and mention the one-parent limit.
  • Claims an abstract class has no constructor You can't create one directly, but its constructor runs when a subclass is created and sets up the shared fields.
  • Uses an abstract class just to share a capability That uses up the only parent slot and shuts out types that already have a parent. Use an interface when subclasses share no code or state.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Object-Oriented Programming cheatsheet.

← Back to all Object-Oriented Programming questions
Pro · $10/mo

81 of 90 Object-Oriented Programming answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime