DEV Community

Cover image for what is an interface and abstract class in c# and its difference?
Santan kr Sharma
Santan kr Sharma

Posted on

what is an interface and abstract class in c# and its difference?

Think of an interface as a super strict boss who gives a list of tasks to his employees (classes) and expects them to complete all of those tasks perfectly.

The boss (interface) only knows what needs to be done, but he has no idea how it should be done. So, he hands off the job description (a list of methods, properties, and events) to his employees (classes) and says "Get it done, I don't care how, just make sure you follow the instructions."

And that's exactly what classes do! They take the job description from the boss (interface), and they come up with their own ways of completing the tasks. The boss is happy as long as all the tasks are completed, and the employees are happy because they get to use their own creativity to solve the problems.

Now, let's talk about abstract classes. Imagine an abstract class as a boss who is a bit more involved in the process. He gives his employees (classes) a list of tasks to do, but also provides some guidance on how to do those tasks. He says "I have a general idea of how these tasks should be done, but feel free to use your own creativity and come up with your own solution."

The key difference between an interface and an abstract class is that an interface only defines what needs to be done, whereas an abstract class provides both the what and the how.

So, to summarize, an interface is like a boss who delegates tasks to his employees, but has no idea how they should be done. And classes are like employees who complete the tasks according to their own methods.

In technological terms:-

INTERFACE
An interface in C# is a blueprint for a class. It defines a set of properties, methods, and events that must be implemented by any class that implements the interface. Interfaces provide a way to define a contract between a class and its clients. The clients can use this contract to interact with objects of the class without needing to know the underlying implementation.

An interface contains only the signatures of methods, properties, and events, but not the implementation. The implementation of the members of an interface is provided by the classes that implement the interface.

Interfaces are useful for creating loosely-coupled systems, as the implementation of an interface can be changed without affecting the code that depends on the interface. This makes it easier to maintain and evolve the system over time.

interface <interface_name> 
{
  // properties, methods, and events
}
Enter fullscreen mode Exit fullscreen mode

A class implements an interface using the "implements" keyword:

class MyClass : <interface_name>
{
  // implementation of properties, methods, and events
}
Enter fullscreen mode Exit fullscreen mode

ABSTRACT CLASS
An abstract class in C# is a class that cannot be instantiated on its own, but must be derived from by another class. An abstract class provides a base implementation of properties, methods, and events that can be shared by multiple derived classes.

An abstract class can contain both abstract and non-abstract members (methods and properties). Abstract members are declared with the "abstract" keyword and do not have a body. The derived classes must implement these abstract members, providing their own implementation.

Abstract classes are useful when you want to provide a common base implementation for multiple classes, but still allow for some variation in the implementation between the classes.

abstract class <abstract_class_name> 
{
  // abstract and non-abstract members
}

Enter fullscreen mode Exit fullscreen mode

A class that derives from an abstract class uses the ":” operator:

class MyClass : <abstract_class_name>
{
  // implementation of abstract members and additional members
}

Enter fullscreen mode Exit fullscreen mode

In conclusion, interfaces and abstract classes are important concepts in C# that provide a way to define contracts and create reusable code. Interfaces are used to define a set of methods and properties that a class must implement, while abstract classes provide a base implementation that can be shared by multiple derived classes.

Choosing between an interface and an abstract class depends on the specific needs of your project and the relationships between the classes involved. Interfaces are best used when you want to define a contract for multiple classes to implement, whereas abstract classes are best used when you want to provide a common base implementation that can be shared by multiple derived classes.

It's important to understand the differences between interfaces and abstract classes, as well as when and how to use them effectively, in order to write maintainable, scalable, and efficient code.

Thanks for you time.
have a good day.

Top comments (0)