Dot Net For All

Why do we use C# Abstract class

In one of my previous article I have described about why do we use interface in C#But in C# we have very important class known as abstract class. Abstract class can have one or more methods which can be abstract(only signature).

But interface only contain the method signature. Then how is an abstract class different from an interface in C#. It is one of the favourite question in C# interview. What is 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 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 abstract methods It should have only 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 of the 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));
        }
    }

We should be aware that abstract class can have its implementation for the methods. In the above code I have create a abstract base class named DataSource. This class is derived to the concrete classes i.e. MsSqlDataSource and OracleDataSource.

The concrete class will have its way to open connection. But there should be a common way to get the connection string for config file.

In our application there can be chance that we have to use different datasource like Ms SQL server, Oracle server or may be excel file. In the above code I have a private method for getting, datasource connection string from the config file based on the data source name and environment (e.g. DEV, QA or PROD).

Now if execute the below code .

            DataSource sqlDS = new MsSqlDataSource("DEV");
            sqlDS.OpenAndReturnConnection();

I will get the following output

Here I am getting the connection string for MsSql for DEV environment. This functionality is common for all the classes derived from DataSource class. And creation of Connection is specific to the derived class. Therefore we have an abstract method in abstract base class.

Though it is very basic and small example. But it can help you to understand the use of abstract class.

Conclusion:

In this article I have discussed about the abstract class and its differences with interface. The article also include a small example which can make understanding about the abstract class clear.

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview