C# Lessons

Abstract class vs interface #L1

Beginner programmers often have a problem understanding the differences between the abstract class and the interface. In this post I will try to explain what these differences are.

What is the abstract class?

An abstract class is a class for which we can not create an object. Just like an ordinary class, it can contain the body of a method (the exception from that are abstract methods that can not contain their body).
The most popular example illustrating the use of an abstract class is the calculation of the surface area of geometric figures, e.g. polygons.
It works like this:
We create an abstract class of polygon. In this class, we create an abstract method CalculateField() and 2 class fields for side lengths. For each type of figure (triangle, square etc.), we create a class that inherits from our abstract class and we add fields that are missing in the base class.
For example – when the polygon has 5 sides, we need to add 3 fields and implement the CalculateField() method for this particular case (which in this case will be pentagon). We do the same for other polygons, in each case we implement another method CalculateField().

What is the interface?

The interface is a similar structure to the class, with the difference that it has only method declarations and does not implement them. We can’t create an object that will be an interface instance. The class, on the other hand, can implement the interface.
The class that implements the interface must have implementations of all interface components, It means that the interface “tells” the class what to include (what methods and properties).
There are several elements which interface can’t contain: constants, fields, operators, constructors/destructors and nested types.

What are the differences?

This is a very popular question on .NET developer recruitment interviews. So if you are looking for a job as a .NET dev, it’s good for you to remember that:

  • The “normal” class can implement one or many interfaces, but can inherit only one abstract class.
  • The interface can only inherit from other interfaces. Abstract class can be inherited from abstract classes, interfaces and even ordinary classes
  • The components of the interface can’t be marked with an access attribute (default is public) and in abstract class they can be.
  • If you’ll add a new method to the interface, you’ll need to check all classes that implement the interface and add the implementation of the new method. If a new class method inheriting from a given abstract class is added to the abstract class, they will be able to use the implementation in the base class so the code will work correctly.
  • Something very simple: we can’t declare fields in the interface and in the abstract class – we can declare fields.
  • The interface can’t have a constructor. In the abstract class – we can provide the implementation of the default constructor.
  • The interface allows only declaration of its components (without implementation). The abstract class allows both the declaration of components and full implementation (which may, however, be obscured in derived classes).

Okay, so that’s all what comes to my mind about abstract classes and interfaces.
Maybe I forgot about something? Or I made a mistake somewhere?
Please, let me know in the comments! 🙂

Leave a Reply