Abstract Class vs Interface

Probably "Difference Between Abstract Class and Interface" is the most frequent question being asked in the .Net world. In this tutorial, I will explain the difference theoretically followed by a code snippet.

Theoretically, there are basically 5 differences between Abstract Class and Interface which are listed below.

Step 1. A class can implement any number of interfaces but a subclass can at most use only one abstract class.

Step 2. An abstract class can have non-abstract Methods(concrete methods) while in the case of Interface, all the methods have to be abstract.

Step 3. An abstract class can declare or use any variables while an interface is not allowed to do so.

So the following code will not compile.

interface TestInterface 
{ 
    int x = 4;  // Field Declaration in Interface 
    void getMethod(); 
    string getName(); 
} 

abstract class TestAbstractClass 
{ 
    int i = 4; 
    int k = 3; 
    public abstract void getClassName(); 
}

It will generate a compile-time error.

Error 1 Interfaces cannot contain fields.

So we need to omit Field Declaration in order to compile the code properly.

interface TestInterface  
{  
    void getMethod();  
    string getName();  
}  

abstract class TestAbstractClass  
{  
    int i = 4;  
    int k = 3;  
    public abstract void getClassName();  
}  

The above code compiles properly as no field declaration is there in Interface.

Step 4. An abstract class can have a constructor declaration while an interface can not do so.

So the following code will not compile.

interface TestInterface
{
    // Constructor Declaration
    public TestInterface()
    {
    }
    void getMethod();
    string getName();
}

abstract class TestAbstractClass
{
    public TestAbstractClass()
    {
    }
    int i = 4;
    int k = 3;
    public abstract void getClassName();
}

The above code will generate a compile-time error.

Error 1 Interfaces cannot contain constructors.

So we need to omit constructor declaration from the interface in order to compile our code.

The following code compiles perfectly.

interface TestInterface 
{ 
    void getMethod(); 
    string getName(); 
} 

abstract class TestAbstractClass 
{ 
    public TestAbstractClass() 
    { 
    } 
    int i = 4; 
    int k = 3; 
    public abstract void getClassName(); 
} 

Step 5. An abstract Class is allowed to have all access modifiers for all of its member declarations while in the interface we can not declare any access modifier(including public) as all the members of the interface are implicitly public.

Note here I am talking about the access specifiers of the member of the interface and not about the interface.

The following code will explain it better.

It is perfectly legal to provide access to specifier as Public (Remember only public is allowed).

public interface TestInterface 
{ 
    void getMethod(); 
    string getName(); 
}

The above code compiles perfectly.

It is not allowed to give any access specifier to the members of the Interface.

interface TestInterface 
{ 
    public void getMethod(); 
    public string getName(); 
}

The above code will generate a compile-time error.

Error 1 The modifier 'public' is not valid for this item.

But the best way of declaring the Interface will be to avoid access specifiers on the interface as well as members of the interface.

interface Test
{
    void getMethod();
    string getName();
}


Similar Articles