Abstract Class In C#

An abstract class in C# is a class that can't be instantiated. Here learn how to declare and implement abstract classes in C# applications.

C# Abstract Class

 
An abstract class is an incomplete class or special class we can't be instantiated. The purpose of an abstract class is to provide a blueprint for derived classes and set some rules what the derived classes must implement when they inherit an abstract class.
 
We can use an abstract class as a base class and all derived classes must implement abstract definitions. An abstract method must be implemented in all non-abstract classes using the override keyword. After overriding the abstract method is in the non-Abstract class. We can derive this class in another class and again we can override the same abstract method with it.
 

C# Abstract Class Features

  1. An abstract class can inherit from a class and one or more interfaces.
  2. An abstract class can implement code with non-Abstract methods.
  3. An Abstract class can have modifiers for methods, properties etc.
  4. An Abstract class can have constants and fields.
  5. An abstract class can implement a property.
  6. An abstract class can have constructors or destructors.
  7. An abstract class cannot be inherited from by structures.
  8. An abstract class cannot support multiple inheritance.
Example 1
  1. #region  
  2.   
  3. //An abstract calss can inherit from a class and one or more interfaces.  
  4.   
  5. interface IVendorTransDetails  
  6. {  
  7.     void getVendorID();  
  8. }  
  9. interface IClaimsTracker  
  10. {  
  11.     void getSeqID();  
  12. }  
  13. class ClaimsMaster  
  14. {  
  15.     string getDCNNO()  
  16.     {  
  17.         return "PC20100308A00005";  
  18.     }  
  19. }  
Example 2
  1. abstract class Abstract : ClaimsMaster, IClaimsTracker, IVendorTransDetails  
  2. {  
  3.     //Here we should implement modifiers oterwise it throws complie-time error  
  4.     public void getVendorID()  
  5.     {  
  6.         int s = new int();  
  7.         s = 001;  
  8.         Console.Write(s);  
  9.     }  
  10.   
  11.     public void getSeqID()  
  12.     {  
  13.         int SeqID = new int();  
  14.         SeqID = 001;  
  15.         Console.Write(SeqID);  
  16.     }  
  17. }   
  18. #endregion  
Example 3

  1.     #region  
  2.   
  3.     //An abstract class can implement code with non-Abstract methods.  
  4.   
  5. abstract class NonAbstractMethod  
  6. {  
  7.     //It is a Non-abstract method we should implement code into the non-abstract method on the class.   
  8.     public string getDcn()  
  9.     {  
  10.         return "PS20100301A0012";  
  11.     }   
  12.     public abstract void getSeqID();   
  13. }   
  14. class Utilize : NonAbstractMethod  
  15. {  
  16.     public override void getSeqID()  
  17.     {  
  18.     }  
  19.   
  20. }   
  21. #endregion  
Example 4
  1. #region   
  2. //Abstract class can have modifiers for methods,properties and An abstract class can implement a property  
  3.   
  4. public abstract class abstractModifier  
  5. {  
  6.     private int id;   
  7.     public int ID  
  8.     {  
  9.         get { return id; }  
  10.         set { id = value; }  
  11.     }   
  12.     internal abstract void Add();  
  13. }   
  14. #endregion  
Example 5
  1. #region   
  2. //Abstract class can have constant and fields  
  3. public abstract class ConstantFields  
  4. {  
  5.     public int no;  
  6.     private const int id = 10;  
  7. }  
  8. #endregion
Example 6
  1. #region   
  2. //An abstract class can have constructors or destructors  
  3. abstract class ConsDes  
  4. {  
  5.     ConsDes()  
  6.     {  
  7.     }  
  8.     ~ConsDes()  
  9.     {  
  10.     }  
  11. }  
  12. #endregion  
Example 7
  1. #region   
  2. //An abstract class cannot be inherited from by structures  
  3. public struct test  
  4. {  
  5. }  
  6. //We can't inheritance the struct class on the abstract class  
  7. abstract class NotInheritanceStruct  
  8. {  
  9. }  
  10. #endregion  
Example 8
  1. #region   
  2. //An abstract class cannot support multiple inheritance  
  3. class A  
  4. {  
  5. }  
  6. class B : A  
  7. {  
  8. }  
  9. abstract class Container : B //But we can't iherit like this : A,B  
  10. {  
  11. }  
  12. #endregion  

Conclusion

 
In this article I have given some simple examples of how to use abstract classes in C#. 
 
Here is a detailed article on Abstract Classes In C#. 
 
Here is another related reading: Method Overloading and Overriding In C#