Why Do We Use Abstract Class?

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?

The article first appeared on DotNet for all

You can learn more about virtual, override, new and abstract keywords here.

Abstract class and Interface Difference

Abstract ClassInterface
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 classConcrete class can implement many interfaces
It can or cannot contain the abstract methodsIt 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.

Abstract class implementation
  1. public abstract class DataSource  
  2. {  
  3.     protected string dataSourceName;  
  4.     private string environment;  
  5.     protected DataSource(string environment, string dsName) {  
  6.         this.environment = environment;  
  7.         this.dataSourceName = dsName;  
  8.         GetDataSourceCredentials();  
  9.     }  
  10.     private void GetDataSourceCredentials() {  
  11.         Console.WriteLine(string.Format("Get {0}'s connection setting for {1} environment from config file", dataSourceName, environment));  
  12.     }  
  13.     public abstract void OpenAndReturnConnection();  
  14. }  
  15. public class MsSqlDataSource: DataSource {  
  16.     public MsSqlDataSource(string environment): base(environment, "MsSQL") {}  
  17.     public override void OpenAndReturnConnection() {  
  18.         Console.WriteLine(string.Format("Create and return Connection for {0} dataSource", dataSourceName));  
  19.     }  
  20. }  
  21. public class OracleDataSource: DataSource {  
  22.     public OracleDataSource(string environment): base(environment, "Oracle") {}  
  23.     public override void OpenAndReturnConnection() {  
  24.         Console.WriteLine(string.Format("Create and return Connection for {0} dataSource", dataSourceName));  
  25.     }  
  26. }  
We should be aware that an abstract class can have its implementation for the methods. In the code, given above, I have created an abstract base class, named DataSource. This class is derived to the concrete classes i.e. sSqlDataSource and OracleDataSource. 

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:

  1. DataSource sqlDS = new MsSqlDataSource("DEV");  
  2. sqlDS.OpenAndReturnConnection();  
I will get the following output:

output
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.


Similar Articles