Sobes.tech
Junior

What is the difference between abstract classes and interfaces?

sobes.tech AI

Answer from AI

Abstract class:

  • Can contain abstract (without implementation) and concrete (with implementation) methods.
  • Can contain fields (instance variables).
  • Can have constructors.
  • Allows single inheritance (one class can inherit from one abstract class).
  • Used to define a base structure and common behavior for related classes.

Interface:

  • Can contain only abstract methods (some languages, like Java from version 8, allow default and static methods with implementation).
  • Cannot contain instance fields (only constants).
  • Cannot have constructors.
  • Allows multiple implementation (one class can implement multiple interfaces).
  • Used to define a contract that classes are obliged to implement.