Abstract Class vs. Interface — C#

This article will cover some of the main differences between an abstract class and an interface in C#. Abstract classes and interfaces are both used to create abstraction. Abstraction in C# is the process of defining the essential details to call operations (i.e., methods) without including the implementation details.

Abstract Class vs. Interface

These are the main differences between an abstract class and an interface that you will run into when using them.

Access Specifier

An interface can not specify the access level for methods or properties like protected or private, and they are public by default. An abstract class can limit the access level for methods and properties. The only exception is an abstract method that can not be private.

Constructors

An interface can not have the declaration of constructors. If attempted, it treats them as different methods making the script implement both. An abstract class can have the declaration of constructors.

Fields

The interface can not have standard fields. They have to be properties with a get and set. The abstract class can have standard fields and properties.

Implementation

An interface only has abstract methods, which means it can only have the method’s signature and not the implementation. An abstract class can have complete methods and abstract methods.

Inheritance

C# doesn’t support multiple inheritances. A class can only inherit from one other class, but we can get around this by using interfaces. A class can inherit from multiple interfaces.

Making Changes

When making changes to an interface, all inheriting scripts have to be changed to implement the changes or throw an error. When making changes to an abstract class, unless the added change was an abstract method, there is no need to change any other code since you can add the base implementation.

Both interfaces and abstract classes are great object-oriented programming concepts. Which one you use depends on the need of the method implementations. An interface is used when you need the signature of a method and not the implementation. An abstract class is used when you do require a base implementation of the method.

Unity Developer, Software Engineer, Game Developer