Interface vs. Abstract Classes

When developing games, you’ll find yourself in a situation in which some code snippets can be shared. 2 ways to do that are using abstract classes and interfaces, but when to use each one?

Abstract class

  • Used as a base class for the same type of objects (for example, enemies)
  • Can have methods, properties, events, and indexers
  • Can declare and assign values to variables and implement methods
  • Can have abstract methods (with empty body, forced to be implemented by child classes)
  • Can contain different access modifiers such as public, private and protected
  • A class can inherit only one abstract class
  • With polymorphism, method’s implementation can be shared, modified or cancelled
  • Can be fully or partially implemented
  • Faster performance

Interfaces

  • Only can declare methods, properties, events, and indexers
  • A class can inherit from multiple interfaces
  • It contains mandatory public access modifier because access to everything in the interface must be allowed from any other script
  • Slower performance because it needs to search and find the corresponding class and method
  • Can contain methods, properties, events, and indexers
  • It is required, mandatory to be fully implemented (it is like a contract)
  • It can be used for multiple types of objects (for instance, IDamageable can be used for player and enemies)

Important additional information:

Since C# 8.0, interfaces can also include:

An example can be found here