Some important facts about Abstract classes

Abstract class declared inside namespace should be either public or internal hence below code will throw compilation error.
  1. using System;        
  2. namespace ConsoleApplication3         
  3.  {    
  4.     private abstract class AbstractClass       
  5.     {}     
  6.   }      

Abstract classes can’t be instantiated hence if you try to execute below code it will throw compilation error.
  1. using System;        
  2. namespace ConsoleApplication3        
  3. {        
  4.    public abstract class AbstractClass        
  5.    {}        
  6.    class DeriveAbstract : AbstractClass        
  7.     {          
  8.        AbstractClass abstract_class = new AbstractClass();        
  9.     }     
  10. }  
Abstract class constructors are called when we create instance of derive class and they are called before derive class constructor.
    1. using System;    
    2. namespace ConsoleApplication3    
    3.  {    
    4.     public abstract class AbstractClass    
    5.     {    
    6.        protected AbstractClass()    
    7.        {    
    8.          Console.WriteLine("protected abstract class constructor");    
    9.        }      
    10.      }    
    11.    class DeriveAbstract : AbstractClass    
    12.    {    
    13.       protected DeriveAbstract()    
    14.       {    
    15.          Console.WriteLine("Public derive class constructor");    
    16.       }    
    17.       static void Main(string[] args)    
    18.      {    
    19.         DeriveAbstract deriveclass = new DeriveAbstract();    
    20.      }    
    21.    }    
    22. }    




No point in creating private constructor inside an abstract class as we can’t even use it inside the derive class. Even creating public constructor inside abstract class doesn’t make sense hence protected access modifier is preferred.

Abstract methods or properties can’t be private and the derive class should implement all the abstract methods and properties using the override keyword.

  1. using System;  
  2. namespace ConsoleApplication3  
  3. {  
  4.     public abstract class AbstractClass  
  5.     {  
  6.         private string name;  
  7.         public string Name  
  8.         {  
  9.             get  
  10.             {  
  11.                 return this.name;  
  12.             }  
  13.             set  
  14.             {  
  15.                 value = this.name;  
  16.             }  
  17.         }  
  18.         protected abstract string Salary { getset; }  
  19.         public void DisplayName(string name)  
  20.         {  
  21.             Console.WriteLine("Name Entered is : {0}", name);  
  22.         }   
  23.         protected abstract void DisplayTotalSalary(long salary);  
  24.     }  
  25.     class DeriveAbstract : AbstractClass  
  26.     {  
  27.         private string salary;  
  28.         protected override string Salary  
  29.         {  
  30.             get  
  31.             {  
  32.                 return this.salary;  
  33.             }  
  34.             set  
  35.             {  
  36.                 value = this.salary;  
  37.             }  
  38.         }  
  39.         protected override void DisplayTotalSalary(long salary)  
  40.         {  
  41.             long totalSalary = salary * 12;  
  42.             Console.WriteLine("Total salary : {0}", totalSalary);  
  43.         }  
  44.         static void Main(string[] args)  
  45.         {  
  46.             DeriveAbstract deriveclass = new DeriveAbstract();  
  47.             deriveclass.DisplayName("Elima Tripathy");  
  48.             deriveclass.DisplayTotalSalary(50000);  
  49.         }  
  50.     }  
  51. }  


As per MSDN the purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.

For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class.