Abstract Class and Interface in C#

Firat Tonak
4 min readJan 2, 2023

--

We will touch on Abstract class and Interface in this article because you might be asked many questions about those.

What is the difference between an abstract class and an interface?

  1. We use the abstract keyword to create an abstract class, however, we use the interface keyword to create an interface class.

IProductInterface.cs

namespace Abstract_Interface;

public interface IProductInterface
{
}

ProductAbstract.cs

namespace Abstract_Interface;

public abstract class ProductAbstract
{

}

2. An abstract class includes the declaration and definition but an interface only includes the declaration, not the definition.

After C# 8, the interface can include declarations as well. (which is called the default interface method) However, this is not the common usage of the interface.

ProductAbstract.cs

namespace Abstract_Interface;

public abstract class ProductAbstract
{
public ProductAbstract()
{

}
private string? _productName;
public string? ProductName
{
get { return _productName; }
set { _productName = value; }
}
public abstract decimal ReturnPrice();

public string ReturnProductName()
{
return ProductName ?? "";
}

}

IProductInterface.cs

namespace Abstract_Interface;

public interface IProductInterface
{
decimal ReturnPrice();
string ReturnProductName();

void TestDefinition()
{
// default interface method.
// This method will never break your functionality
// In general, the default interface method is used to implement the methods in the future
}
}

3. Abstract class does not support multiple inheritances, however, the interface supports multiple inheritances.

When you try to derive a class from multiple abstract classes, you will get an error like the one below

LaptopProduct.cs

namespace Abstract_Interface;

public class LaptopProduct : ProductAbstract,IProductInterface,IBasketInterface
{
public override decimal ReturnPrice()
{
throw new NotImplementedException();
}
}

Another important question is when we will use an abstract class and an interface

Basically, we are using abstraction at the design level which is why if you are sure about the definition, you should use abstract class otherwise you should use interface.

Let’s say you want to have a method which is named TypeOfProduct and you want to make it concrete, then you should use an abstract class and define it in the class. If you do it, the definition of the method cannot be changed from derived classes.

However, if you are not sure about the definition, you should create the TypeOfProduct method without a definition in the interface and all derived classes can implement their logic

LaptopProduct.cs

namespace Abstract_Interface;

public class LaptopProduct : ProductAbstract,IProductInterface, IBasketInterface
{
public override decimal ReturnPrice()
{
throw new NotImplementedException();
}

public void TypeOfProduct() // coming from IProductInterface
{
throw new NotImplementedException();
// you can put your logic here
}
}

As you can see above, you can change the logic of the method that is coming from the IProductInterface.

However, if you check the ProductAbstract.cs, you define the TypeOfProduct method, and cannot be changed from the derived classes.

The type of product could be anything depending on the type of class like a keyboard, a mouse, and so on.

namespace Abstract_Interface;

public abstract class ProductAbstract
{
public ProductAbstract()
{

}
private string? _productName;
public string? ProductName
{
get { return _productName; }
set { _productName = value; }
}
public abstract decimal ReturnPrice();

public string ReturnProductName()
{
return ProductName ?? "";
}

public void TypeOfProduct()
{
Console.Write("Type of product"); // you cannot change this from the derived class
}

}

As you can see, the abstract class and the interface are similar but we use interfaces most of the time at the development level because;

  1. Interfaces make our project flexible (like the TypeOfProduct method)
  2. Interfaces are contracts which means whenever a class is derived from an interface, the derived class has to implement methods that are coming from the interface.

The interface says that the methods that come from the interface will be implemented for sure once you use the interface, however, the abstract does not say that the methods will be implemented for sure

We are using abstract class at the design level which is why you cannot create an instance of an abstract class as well as this is the same for interfaces.

When you try to create an instance of an abstract class or an interface you will get an error as below.

Also, the abstract class can have a constructor, however, the interface cannot have it.

In this article, we learned some important topics related to abstract and interface. I hope, it will be useful.

Code: https://github.com/frttnk/OOP-and-C-sharp-with-Most-Common-Interview-Questions-Abstract-Class-and-Interface

--

--

Firat Tonak

Seasoned Senior Full-Stack .NET Developer sharing hands-on experience and cutting-edge technologies in comprehensive Full-Stack development. #TechInsights