C#

Should we use interfaces or abstract classes? (and how to choose between the two)

In this article, I will explain how to choose between interfaces and abstract classes to write code in C#.
This decision is important because it affects code and usability later.

Like any developer, at some point you should have asked yourself the question: should I rather create an interface or an abstract class for this new guy? How to choose between the two? What are the consequences if I am wrong?
Rest assured, we will talk about all of this in this article.

I often meet beginners who use interfaces everywhere. It’s fashionable, so they put it all over the code, even when it’s not necessary. The most classic example I often see: creating an interface for a single class. And when I ask why the developer made this choice, I often have some vague answers.

What is an interface in C#?

In C#, an interface makes it possible to describe a “contract”.
It can only contain methods, properties, or events. The members of an interface are necessarily public.

A class can then “implement” an interface. That is, it undertakes to implement the methods described in the interface.

In C#, a class can implement several different interfaces.

What is an abstract class in C#?

An abstract class is a partially defined class. It includes, at least, an abstract method that has no body (no code).

An abstract class can contain all elements of a “normal” class: fields (variables), events, properties, methods …

On the other hand, an abstract class can have a state. Since it can contain (variable) members, it is possible to assign a state to this class.

In C#, a class can inherit only one other class.

What are the differences between interfaces and abstract classes in C#?

As we have seen in the preceding paragraphs, abstract classes are partially defined classes.

With respect to an interface, an abstract class can have:

a state (fields – variables)
behavior (code)
A class can:

  • Implement one or more interfaces
  • Inherit one class (abstract or not)

How to choose between interface or abstract class?

An abstract class is usually used to construct similar classes. They will all have an implementation in common, that of the abstract class.

An interface is usually used to define capabilities (the “contract”), even though classes do not have much in common.

To help you, ask yourself the following questions: Will my classes have a common behavior?

Example in .NET

In the .NET framework, you have dozens of classes that implement IEnumerable. This allows you to use foreach on a collection of objects.

If you look at it more closely, classes that implement IEnumerable sometimes have nothing in common. On the other hand, you can enumerate collections of integers or collections of chains.

Do these classes have a common behavior? The answer is no. Integers and chains do not have much in common (at least from the point of view of enumeration).

For abstract classes, I propose the following example. We have an AbstractLogger base class. This contains methods “Info”, “Error”, “Warn” … Here is the corresponding code:

As we see, the AbstractLogger class offers a partial implementation.
If we want to create a logger for the console, we could write something like this:

We could of course create a class to write to a file or another to write to the database. All of these classes would share the same default behavior (the Info, Warn, and Error methods).

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.