Abstract Class Vs Interface - C#

Article Overview

  • Background
  • Difference between Abstract Class and Interface in C#
  • Practical Scenarios
  • Summary

Background

 
Here is a list of the key differences between Abstract Class and Interface in C#. This article will be useful to both beginners and professional C# developers.
 
 

Difference between Abstract Class and Interface in C#

 
Category Abstract Class Interface
What is it? Abstract doesn’t provide full abstraction. Interface provides full abstraction.
How to declare/create? abstract class is used to create Abstract classes. interface is used to create Interfaces.
Can it has fields? Abstract class can have fields. Interfaces can’t have fields.
Implementations of its members/methods? Abstract classes can have implementations for some of its members (methods). Interface can't have implementation for any of its members.
Access modifiers? Abstract class members can have access modifiers. Interface members can’t have access modifiers.
Default access modifiers? Abstract class members can be private by default which can be change. Interface members can be public by default which can not be change.
Is it inherited from another Abstract Class or Interface?
Abstract class can inherit from another abstract class or another interface.
Interface can inherit from another interface only and cannot inherit from an abstract class.
Inherit from another Abstract Class or Interface?
Abstract class can inherit from another abstract class or another interface. Interface can inherit from another interface only and cannot inherit from an abstract class.
 
Now, let us see the above-mentionded points by practical examples as following. 
 

Practical Scenarios

 
Example 1
 
How to create Abstract Classes and Interfaces? 
  1. public abstract class Customer  
  2. {  
  3. }  
  4.   
  5. public interface ICustomer  
  6. {  
  7. }  
Note
  • abstract class keywords are used to create Abstract Classes.
  • interface keyword is used to create Interfaces. 
Example 2
 
How to create Abstract Classes and Interfaces along with fields? 
  1. public abstract class Customer  
  2. {  
  3.     int i1;  
  4. }  
  5.   
  6. public interface ICustomer  
  7. {  
  8.     int i2;//Not possible  
  9. }  
Note
  • Interface will give compile time error “Interfaces cannot contain fields” because it will not allow fields.  
Example 3
 
How to create Abstract Classes and Interfaces along with members? 
  1. public abstract class Customer  
  2. {  
  3.     public abstract void Print1();  
  4. }  
  5.   
  6. public interface ICustomer  
  7. {  
  8.     void Print2();  
  9. }  
Note
  • Abstract keyword is required for members of Abstract Classes. You can use access modifiers. By default it will be private.
  • For members of Interfaces you can’t use access modifiers. By default it will be public.
Example 4
 
How ismember implementation possible in Abstract Classes and Interfaces? 
  1. public abstract class Customer  
  2. {  
  3.     public void Print1()  
  4.     {  
  5.     }  
  6. }  
  7.   
  8. public interface ICustomer  
  9. {  
  10.     void Print2()//Not possible  
  11.     {  
  12.     }  
  13. }  
Note
  • Interface will give error Abstract “interface members cannot have a definition” 
Example 5
 
How to implement abstract or interface members in inherited class? 
  1. public abstract class Customer  
  2. {  
  3.     public abstract void Print1();  
  4. }  
  5.   
  6. public interface ICustomer  
  7. {  
  8.     void Print2();  
  9. }  
  10.   
  11. public class MainClass : Customer, ICustomer  
  12. {  
  13.     public override void Print1()  
  14.     {  
  15.     }  
  16.     public void Print2()  
  17.     {  
  18.     }  
  19. }  
Note
  • override keyword is used to implement abstract members.
  • public access modifier is must for interface member implementation. 
Example 6
 
How to implement inherit from another Abstract Class or Interface to Abstract Class or Interface? 
  1. public abstract class Customer1  
  2. {  
  3. }  
  4.   
  5. public abstract class Customer2 : Customer1  
  6. {  
  7. }  
  8.   
  9. public abstract class Customer3 : Customer1, ICustomer1  
  10. {  
  11. }  
  12.   
  13. public interface ICustomer1  
  14. {  
  15. }  
  16.   
  17. public interface ICustomer2 : ICustomer1  
  18. {  
  19. }  
  20.   
  21. public interface ICustomer3 : Customer1 //Not possible  
  22. {  
  23. }  
Note
  • An interface can inherit from another interface only and cannot inherit from an abstract class, whereas an abstract class can inherit from another abstract class or another interface.
  • Therefore, interface ICustomer3 can not inherit to abstract class Customer1.

Summary

 
Now, I believe you will be able to know the key difference between Abstract Class and Interface in C#.