In one of my previous articles, I described about why we use interface in C#. In C#, we have a very important class known as an abstract class. An abstract class can have one or more methods, which can be abstract (only signature). The interface only contains the method signature. How is an abstract class different from an interface in C#? It is one of the favorite questions in C# interviews: What is an abstract class and why do we use it?
You can learn more about virtual, override, new and abstract keywords here.
Abstract class and Interface Difference
| Abstract Class | Interface |
| We cannot create an instance of the this class. | Interface can be only variable type and not instance. |
| It can have constructor. | It cannot have constructor. |
| It can be derived to some other class. | It is created to be derived by other class. |
| It can have implementation(non abstract) of one or more methods. | It cannot have function definition. |
| Concrete class can implement only one abstract class | Concrete class can implement many interfaces |
| It can or cannot contain the abstract methods | It should only have the method signatures. |
| It can have private, protected, internal data members. | All the members are public by default. |
| It cannot be derived to a structure. | It can be derived by a structure. |
Practical Implementation Of Abstract Class
Lets talk about the practical implementation of the abstract class. Most people are very much aware about the theory of the these classes but as far as implementation is concerned, they are not sure about it.
- public abstract class DataSource
- {
- protected string dataSourceName;
- private string environment;
- protected DataSource(string environment, string dsName) {
- this.environment = environment;
- this.dataSourceName = dsName;
- GetDataSourceCredentials();
- }
- private void GetDataSourceCredentials() {
- Console.WriteLine(string.Format("Get {0}'s connection setting for {1} environment from config file", dataSourceName, environment));
- }
- public abstract void OpenAndReturnConnection();
- }
- public class MsSqlDataSource: DataSource {
- public MsSqlDataSource(string environment): base(environment, "MsSQL") {}
- public override void OpenAndReturnConnection() {
- Console.WriteLine(string.Format("Create and return Connection for {0} dataSource", dataSourceName));
- }
- }
- public class OracleDataSource: DataSource {
- public OracleDataSource(string environment): base(environment, "Oracle") {}
- public override void OpenAndReturnConnection() {
- Console.WriteLine(string.Format("Create and return Connection for {0} dataSource", dataSourceName));
- }
- }
The concrete class will have its way to open the connection. There should be a common way to get the connection string for config file.
In our Application, there can be a chance that we have to use different data sources like Ms SQL server, Oracle server or maybe MS Excel file. In the code, mentioned above, I have a private method for getting the datasource connection string from the config file, based on the data source name and environment (e.g. DEV, QA or PROD).
Now, if you execute the code, given below:
- DataSource sqlDS = new MsSqlDataSource("DEV");
- sqlDS.OpenAndReturnConnection();

Here, I am getting the connection string for DEV environment. This functionality is common for all the classes derived from DataSource class. The creation of connection is specific to the derived class. Therefore, we have an abstract method in abstract base class.
Though, it is a very basic and small example but it can help you to understand the use of an abstract class.
Conclusion
In this article, I have discussed about the abstract class and its differences with an interface. The article also includes a small example , which makes you understand about the abstract class.

Prakash TripathiPosted Jul 13, 2016, 2:42 AM
Simple and easy example to understand. In short, if there is some common piece we want to keep that is required in all the derived class, go for abstract class else go for interface.
Nagaraj SPosted Jul 12, 2016, 10:37 AM
@Vikram Chaudhary Very good article. I have one question. In your example, I can get the same result just by Declaring the "DataSource" Class as an "instant Class". But why would you need to go for Abstract Class?. What problem does it solves?
Rahul Pushpendu BhaskarPosted Jul 12, 2016, 9:38 AM
Nice Article...Thank you
kalu singh raoPosted Jul 12, 2016, 1:45 AM
Nice...
Nikhil SanganiPosted Jul 12, 2016, 12:41 AM
Nice one.