Advantage of abstract class over Interface in C#.Net

By | December 12, 2017

Advantage of abstract class over Interface in C#.Net

Adding new functions/methods to existing interface is difficult as compared to adding it to abstract class.

If you want to add a new functions/methods to existing interface, you must have to implement the new functions in all of the classes that implement the changed interface. The code will not work until you make the change.

But if you want to add an additional functions to the Abstract class, you can have default implementation. So no need to implement new function in all of the classes which derived from abstract class. Your code will work without many changes.

For e.g: Suppose we have interface ITest having DoTask() method, and two classes ClassA & Class B which are derived from ITest interface and implementing DoTask method of the Interface as below,

    interface ITest
    {
        void DoTask();
    }

    public class ClassA : ITest
    {
        public void DoTask()
        {
            int a;
        }
    }
    
    public class ClassB : ITest
    {
        public void DoTask()
        {
            int a;
        }
    }

Now, suppose if we add one more method (DOWork) into interface ITest, then all the child classes (A & B) which are derived from ITest interface must have to implement new method, otherwise it will give error like below,

Advantage of abstract class over Interface in C#.Net

Advantage of abstract class over Interface in C#.Net

Now check same example with abstract class,  in this case we can have option of default implementation so no need to implement new method in all of the derived classes. we can have default implemention in base class and wherever required we can override that method in child class.

    //abstract class with abstract method, and its implementation in both derived classes.
    abstract class AbsClass
    {
        void DoTask();
    }

    public class ClassA : AbsClass
    {
        public void DoTask()
        {
            int a;
        }
    }
    
    public class ClassB : AbsClass
    {
        public void DoTask()
        {
            int a;
        }
    }

Now If we add new default method (method with default implementation) in abstract class DoWork(), then no need to add method implementation to its all derived classes.

public abstract class AbsClass
    {
        // abstract method
        public abstract void DoTask();

        //Non- abstract method
        void DoWork()
        {
            int a;
        }
    }

    public class ClassA : AbsClass
    {
        public override void DoTask()
        {
            int a;
        }
    }

    public class ClassB : AbsClass
    {
        public override void DoTask()
        {
            int a;
        }
    }

Summary:

In short Advantage of abstract class over interface is we can easily add new default implemented method to existing abstract class and no need to implement that method in all the child classes.
But if we add new method to existing interface then we need to implement that method in every derived classes.

C# interview questions

Leave a Reply

Your email address will not be published. Required fields are marked *