Difference between abstract class and interface in C#

Introduction

 
In this article, I am going to explain the difference between an Abstract Class and an Interface in C# with some code examples.
 

Abstract class vs interface 

  1. An abstract class doesn't provide full abstraction but an interface does provide full abstraction; i.e. both a declaration and a definition is given in an abstract class but not so in an interface.
  2. Using abstract we cannot achieve multiple inheritance but using an Interface we can achieve multiple inheritance.
  3. We can not declare a member field in an Interface.
  4. We can not use any access modifier i.e. public, private, protected, internal etc. because within an interface by default everything is public.
  5. An Interface member cannot be defined using the keyword static, virtual, abstract or sealed.
Abstract Class
 
We can not create an object of an abstract class and can call the method of abstract class with the help of class name only.
 
Take a look at an Abstract class example,
 
Difference Between Abstract Class And An Interface
 
The code window looks like this,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace ConsoleApplication4  
  6. {  
  7.    abstract class M1  
  8.    {  
  9.         public int add(int a, int b)  
  10.         {  
  11.             return (a + b);  
  12.         }  
  13.     }  
  14.     class M2 :M1  
  15.     {  
  16.         public int mul(int a, int b)  
  17.         {  
  18.             return a * b;  
  19.         }  
  20.     }  
  21.     class test  
  22.     {  
  23.         static void Main(string[] args)  
  24.         {  
  25.             M2 ob = new M2();  
  26.             int result = ob.add(10, 20);  
  27.             Console.WriteLine("the result is {0}", result);  
  28.             Console.ReadLine();  
  29.         }  
  30.     }  
  31. }  
When we run it the output looks like this,
 
Abstract Class in C#
 
An Interface
 
The syntax of an Interface looks like this,
 
interface
{
     //Interface member
}
 
Note
  1. An Interface member cannot contain code bodies.
  2. Type definition members are forbidden.
  3. Properties are defined in an interface with the help of an access block get and set, which are permitted for the property.
    1. Interface myInterface  
    2. {  
    3.     int myint   
    4.     {  
    5.           get;  
    6.            set;  
    7.     }  
    8. }  
Take a look in an interface example,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace ConsoleApplication3  
  6. {  
  7.     interface MyInterface  
  8.    {  
  9.       void myMethod();  
  10.     }  
  11.     class MyClass : MyInterface  
  12.    {  
  13.       public static void Main()  
  14.       {  
  15.             MyClass cls = new MyClass();  
  16.             cls.myMethod();  
  17.       }  
  18.        public void myMethod()  
  19.       {  
  20.             Console.WriteLine("welcome to MCN IT SOLUTION");  
  21.             Console.ReadLine();  
  22.        }  
  23.     }  
  24. }  
After running this program we obtain the output as follows,
 
Interface in C#
 
Note
In C#, an Interface provides only those public services declared in the interface, whereas an abstract class provides the public services defined in an abstract class and those members that are inherited from the abstract class's base class.