Top 6 Differences of Abstract class and Interface in C#

Difference between Abstract class and Interface in C#

Table of Contents

Difference between Abstract class and Interface in C#

Hello folks, today we will discuss regarding Top 6 Differences of Abstract class and Interface in C#.Below are main differences which are discussed using examples and code snippets:

1. A class can implement any number of interfaces but a subclass can inherit only one abstract class, because Multiple inheritance doesn’t support by C#.

               

Interface example:

interface TestInterface1
{
       void Method1();
} 

interface TestInterface2
{
      void Method2();
}

class InheriteInterface : TestInterface1, TestInterface2
{
      public void Method1() { }
      public void Method2() { }
}

Abstract example, in which multiple inheritance is not allowed:

abstract class TestAbstractClass1
{
     public abstract void getClassName();
}
abstract class TestAbstractClass2
{
     public abstract void getClassName();
}

// Can't inherite as Multiple inheritance doen't allowed in C#
class AbstractInherite : TestAbstractClass1, TestAbstractClass2 
{
     public override void getClassName()
     {
       throw new NotImplementedException();
     }
}

2. An abstract class can have non-abstract Methods or concrete methods, while in case of Interface we can’t have implementation for its members.

interface TestInterface1
{
    void Method1();
    void Method2() { } // implementation not allowed inside Interface in C#
}

abstract class TestAbstractClass1
{
     public abstract void Method1();
     public void Method2() { } // implementation is allowed inside abstract
}

Please note: In C# 8.0 Interface can also implement methods. I strongly recommend to refer Microsoft’s Docs Link

3. Interface can’t have fields where as an abstract class have fields.

interface TestInterface1
{
     int iInterface = 10; // can't declare fields, give error
}

abstract class TestAbstractClass1
{
    int iAbstract = 10;
}

4. An interface can inherit from another interface only where as abstract class can inherit from abstract class or any other interface.

interface TestInterface1
{
}
abstract class TestAbstractClass1
{
    int iAbstract = 10;
}

// Abstract class can inherite abstract class as well as single or multiple Interfaces 
abstract class TestAbstractClass2: TestAbstractClass1, TestInterface1
{
    int iAbstract = 10;
}

// Interface can only inherite interfaces, will give error
interface TestInterface2 : TestAbstractClass1
{
}

5. Abstract class members can have access modifiers where as interface members cannot have access modifiers.

Interface’s members are always public access modifiers, while in case of abstract class’s members are private always.

interface TestInterface1
{
    public void method1(); // can't declare access modifiers.
}

abstract class TestAbstractClass1
{
    public abstract void Method1();
}

6. An abstract class can have constructor declaration while an interface can not do so.

interface TestInterface1
{
    public TestInterface1() //Can't declare Constructor in interface.
    { }
    void method1();
}

abstract class TestAbstractClass1
{
    public TestAbstractClass1()
    { }
    public abstract void Method1();
}

7. Speed: Interface requires more time to find the actual method in the corresponding classes, while abstract class is faster as compare to Interface.

8. Homogeneity: If various implementations only share method signatures then it is better to use Interfaces. If various implementations are of the same kind and use common behavior or status then abstract class is better to use.

9. Addition functionality : If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. while in Abstract, if we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.

interface TestInterface1
{
    void M2();
}

abstract class TestAbstractClass1
{
    int iAbstract = 10;
    public abstract void M1();
}

abstract class TestAbstractClass2 : TestAbstractClass1,TestInterface1
{
    int iAbstract = 10;

    //Must have to implement below method
    public void M2()
    {
        throw new System.NotImplementedException();
    }
}

I have implemented live example to understand difference between abstract class and Interface, kindly download it and try to understand real implementation.

Folks you should also refer below articles for better understanding of differences between:

Leave a Reply