What is the difference between an abstract class and an interface, and when would you use each?

Get ready for your Object‑Oriented Programming Test. Use flashcards and multiple-choice questions. Each question includes hints and explanations. Prepare for your exam today!

Multiple Choice

What is the difference between an abstract class and an interface, and when would you use each?

Explanation:
An abstract class can hold data (state) and provide default behavior through concrete methods, while leaving some methods abstract to be implemented by subclasses. This lets you factor shared code and common state for a group of related classes, forming a clear base type they all extend. An interface, by contrast, defines a contract of methods that a class must implement, without tying those classes to any particular implementation or instance state. It enables you to specify capabilities or roles that can be added to unrelated classes, and it supports treating different types uniformly when they share the same interface. So you use an abstract class when you have related classes that should share code and data; you use an interface when you want to express a capability or behavior across unrelated classes, without imposing a common implementation. In many languages, this distinction is reinforced by the fact that interfaces don’t carry instance state, whereas abstract classes can.

An abstract class can hold data (state) and provide default behavior through concrete methods, while leaving some methods abstract to be implemented by subclasses. This lets you factor shared code and common state for a group of related classes, forming a clear base type they all extend.

An interface, by contrast, defines a contract of methods that a class must implement, without tying those classes to any particular implementation or instance state. It enables you to specify capabilities or roles that can be added to unrelated classes, and it supports treating different types uniformly when they share the same interface.

So you use an abstract class when you have related classes that should share code and data; you use an interface when you want to express a capability or behavior across unrelated classes, without imposing a common implementation. In many languages, this distinction is reinforced by the fact that interfaces don’t carry instance state, whereas abstract classes can.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy