How to control using interface vs abstract class in c#.
Tags: microsoft, csharp, dotnet
Published: 2024-03-30 02:00:45

It is true that computers have more power than ever.SDKs are so smart to support developers quickly and effectively. But I assure that crucial knowledge of programming is important. These are building amazing frameworks or super platforms while People mix up what is an interface or an abstract class, when do, why use it.
We are here to clearly make some mistakes. This knowledge is gained today and used to implement an application that is effective and optimized.

**Interface**
An interface is like a contract that a class must follow. The body of interfaces doesn't provide any implementation of these methods. It only defines this type of return, name's method and parameters.
Today, some languages of programming, such as C# 10 [Defaults Methods](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods) or Java which are enable concrete implementation methods. It means these IDEs don't notice that it is invalid.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7ekgqt89jszpdp03tgu.png)

It is a contract, so any class or struct implement interface must provide implementation details.

**Abstract class**
An abstract class as a sort of halfway house between a pure interface and a fully implemented class. It is more flexible than interfaces. We can require implementation in this derived class through abstract functions. These methods can also be overridden if the base class allows for overriding by **virtual** keyword.

Use abstract classes as base common functions to inherit or override. We can change this method to meet with derived classes, while no changes to base functions also impact other classes. 


**Similarly between abstract class and interface** 
In fact, both interfaces and abstract classes cannot be instantiated. We may think that they are not complete. They need more implementations before they can be instantiated.
Another key is that a class can both inherit from a abstract class and implement multiple interfaces.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eenht92g72n2es56hako.png)


**Differences between Abstract class and Interface** 
The main difference between an interface and an abstract class is that an interface cannot have properties. It only supports fields, while an abstract class can have both fields and properties. 

Another key difference is that classes can implement more than one interface, but they can extend only one abstract class. Design classes implement multiple interfaces, which is the Interface Segregation Principle in SOLID. 

Another difference is that interfaces can be implemented by classes or extended by an interface, because an interface extending another interface is not responsible for implementing methods from the parent interface.

**Inclusion**
In summary, the choice between abstraction and interfaces depends on the relationships between classes and the level of implementation detail that you want to provide. Use abstraction when there is a common implementation among related classes, and use interfaces when there is a common contract among unrelated classes.

Abstraction and interfaces are both essential concepts in object-oriented programming. It is important to note that we should mix up definitions. Using it correctly will help optimize your application.