Difference Between Abstract Classes and Interfaces

1. Abstract classes can implement some of its members (Methods or Properties that are not abstract) whereas interfaces cannot. 

  1. abstract class MyClass {  
  2.      private int id;  
  3.      public int Id {  
  4.          set {  
  5.              this.id = value;  
  6.          }  
  7.          get {  
  8.              return this.id;  
  9.          }  
  10.      }  
  11.  }  
If we build our solution we will not get an error.

But if we create an abstract property as in the following:
  1. abstract class MyClass {  
  2.     private int id;  
  3.     public abstract int Id {  
  4.         set {  
  5.             this.id = value;  
  6.         }  
  7.         get {  
  8.             return this.id;  
  9.         }  
  10.     }  
  11. }  
Then when we build our solution we will now get a compile-time error because an abstract member cannot have a body.

error

In an interface, we cannot provide an implementation. For example in the following:
  1. interface IOne {  
  2.    int MyProperty { getset; }  
  3. }  
If we build our solution, we will not get an error.

But if we try to implement it as in the following:
  1. interface IOne {  
  2.     int MyProperty {  
  3.         set {  
  4.   
  5.         }  
  6.         get {  
  7. rn 21;  
  8.         }  
  9.     }  
  10. }  
We will get a a compile-time error as in the following:

compile time error

2. Abstract classes can have fields whereas interfaces cannot.
  1. abstract class MyClass {  
  2.    private int id;  
  3. }  
If we build our solution, we will not get an error.

If we add a member to the interface and build our solution.
  1. interface IOne {  
  2.    public string name;  
  3. }  
We will get an error as in the following:

get any error

3. An abstract class can inherit from another abstract class as well as interfaces whereas interfaces can only inherit from other interfaces.
  1. using System;  
  2.   
  3. namespace AbstractVsInterfaces {  
  4.     class MainClass {  
  5.         public static void Main() {  
  6.   
  7.         }  
  8.        abstract class MyAbstract {  
  9.               
  10.         }  
  11.         interface IOne {  
  12.   
  13.         }  
  14.         interface ITwo {  
  15.   
  16.         }  
  17.         interface IThree {  
  18.   
  19.         }  
  20.         abstract class MyClass : MyAbstract, IOne, ITwo, IThree {  
  21.   
  22.         }  
  23.     }  
  24. }  
If we build our solution, we will not get an error.

In Interfaces
  1. using System;  
  2.   
  3. namespace AbstractVsInterfaces {  
  4.     class MainClass {  
  5.         public static void Main() {  
  6.   
  7.         }  
  8.        abstract class MyAbstract {  
  9.               
  10.         }  
  11.         interface IOne {  
  12.   
  13.         }  
  14.         interface ITwo {  
  15.   
  16.         }  
  17.         interface IThree:IOne,ITwo,MyAb {  
  18.   
  19.         }  
  20.         abstract class MyClass {  
  21.   
  22.         }  
  23.     }  
  24. }  
When we try to inherit the IThree interface from IOne and ITwo, we don't get an error but when we try to do the same from an abstract class we don't even get any intellisense like in the image below.

We will get an error

And if we try to inherit the IThree interface from the class by hardcoding the class name, we get the following error:

We get the following error

4. Abstract class members can have access modifiers whereas interfaces cannot.

There are a few similarities between the two:

 

  • Both of them are incomplete.
  • We cannot create their instances.
  • Both of them can act as base types.


Similar Articles