Abstract class vs. interface in C#
In any technical discussion or in any interview this (abstarct class vs interface) is very popular question. Although abstract classes and interfaces seem similar in some ways, there are key differences that will determine which is the best choice for what you’re trying to accomplish.
If we will summurize the differences between these two then can say that An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.
Abstract Class in C#
The purpose of an abstract class is to provide a blueprint for derived classes and set some rules what the derived classes must implement when they inherit an abstract class.
An abstract class is designed to be inherited by subclasses that either implement or override its methods. In other words, abstract classes are either partially implemented or not implemented at all. You can have functionality in your abstract class—the methods in an abstract class can be both abstract and concrete. An abstract class can have constructors—this is one major difference between an abstract class and an interface. You can take advantage of abstract classes to design components and specify some level of common functionality that must be implemented by derived classes.
abstract class NonAbstractMethod { //It is a Non-abstract method we should implement code into the non-abstract method on the class. public string getDcn() { return "PS20100301A0012"; } public abstract void getSeqID(); } class Utilize : NonAbstractMethod { public override void getSeqID() { } }
public abstract class abstractModifier { private int id; public int ID { get { return id; } set { id = value; } } internal abstract void Add(); }
Interface in C#
An interface includes the declarations of related functionalities. The entities that implement the interface must provide the implementation of declared functionalities.
In C#, an interface can be defined using the interface
keyword. An interface can contain declarations of methods,
properties, indexers, and events. However, it cannot contain fields, auto-implemented properties.
interface IFile { void ReadFile(); void WriteFile(string text); }
You cannot apply access modifiers to interface members. All the members are public by default. If you use an access modifier in an interface, then the C# compiler will give a compile-time error "The modifier 'public/private/protected' is not valid for this item.".
When to use abstarct class and when to use interface in C#
Abstract classes provide you the flexibility to have certain concrete methods and some other methods that the derived classes should implement. By contrast, if you use interfaces, you would need to implement all the methods in the class that extends the interface. An abstract class is a good choice if you have plans for future expansion – i.e. if a future expansion is likely in the class hierarchy. If you would like to provide support for future expansion when using interfaces, you’ll need to extend the interface and create a new one.
On a different note, it is easy to add a new interface to the hierarchy if need be. However, if you already have an abstract class in your hierarchy, you can’t add another—i.e., you can add an abstract class only if none are available. You should use an interface if you want a contract on some behavior or functionality. You should not use an interface if you need to write the same code for the interface methods. In this case, you should use an abstract class, define the method once, and reuse it as needed. Do use interfaces to decouple your application’s code from specific implementations of it, or to restrict access to members of a certain type.