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();
}

Long CuiPosted Jun 12, 2020, 3:36 AM
For yours:It is not allowed to give any access specifier to the members of the Interface.interface TestInterface { public void getMethod(); public string getName(); } Above code will generate a compile time error WHICH IS WRONG.
Ajit Kumar PanditPosted Jun 29, 2016, 3:26 AM
As Mr. Prasoon has been explained in the case (5) that "An abstract Class is allowed to have all access modifiers for all of its member declaration." But it is not true. See the following reasons supporting this: -----------Test(1)- internal abstract int Add3Numbers(); // An abstract member cannot be interal.....................Test(2)- private abstract int RollNumber { get; set; } // An abstract member cannot be private.
chhaviPosted Sep 12, 2012, 1:34 AM
Its good.Really Impressive.
vijay kumareditedPosted Aug 16, 2012, 9:32 AMEdited Aug 16, 2012, 9:33 AM
Nice,can you explain using examples
abhishek sawantPosted Jul 20, 2012, 10:28 AM
really nice
Alex TimoshevskyPosted Jun 25, 2012, 12:57 AM
From MSDN Here are some recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components. If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface. If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes. If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class. If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.
priya rishiPosted Feb 3, 2011, 1:01 AM
Nice Article
Nilay JoshiPosted Sep 9, 2010, 8:41 AM
Hi, I want to add something in point-3. Point-3 states - "An abstract class can declare or use any variables while an interface is not allowed to do so." But interface can have Properties. This point has to be added in Article. Please Correct me if I'm wrong.!!!
akhil kPosted Jul 1, 2010, 1:16 AM
hi, you have written like we cant declare or use variables in interface. i didn't get that.... Bcz i think we can declare and initialize variables in interface............. plz explain plz
surya pradeepPosted Jun 11, 2010, 3:38 PM
appreciable..........
senthil kumaranPosted May 6, 2010, 3:51 AM
Its really good articles...
Hareesh MGPosted Apr 26, 2010, 11:54 AM
It's really a very good article
PrasoonPosted Jun 11, 2009, 12:24 PM
Hi Kanaiyalal Prajapati Thanks for your Response .But in this article I think in the Article at any place there is any Method Defination in any of Interface afcourse declaration is allowed.As far as Variable Defination is concerned which was mentioned in point number 3 ,was to get Reader an insight that neither defination of variables nor defination of method is allowed inside Interface.That was intent of puting Variable defination in Point number 3. Further suggestions are appreciated. Thanks and Regards Prasoon. the