Abstract Class in C#

Look the following points:

  1. An Abstract Class Contains Abstract as well as Non Abstract Methods.

  2. An Abstract Method is the one which has only declaration, its implementation is left to the class which derives the abstract class.

  3. To Declare a Class as Abstract we must use a keyword abstract.

    Example:

    abstract class A

  4. Abstract members can not be private.

    For example:
    1. abstract class P1  
    2. {  
    3.    abstract void Display();   
    4. }   
    This will give a error abstract members can not be private,you can see the below screen shot

    abstract class

  5. So,we must use a public access specifier for abstract method as by default methods are private.
    1. abstract class P1  
    2. {  
    3.    public abstract void Display();   
    4. }   
  6. Class which derives the abstract class needs to implement abstract method of abstract class.
    1. abstract class P1  
    2. {  
    3.     public abstract void Display();  
    4. }  
    5. class P2: P1  
    6. {}  
    This will generate an error P2 does not implement inherited abstract member P1.Display() .

    Look at the below image:

    Code

  7. So the class which inherits abstract class needs to implement abstract method of abstract class.
    1. abstract class P1  
    2. {  
    3.     public abstract void Display();  
    4. }  
    5. class P2: P1  
    6. {  
    7.     public override void Display()  
    8.     {  
    9.         Console.WriteLine("Hi");  
    10.     }  
    11. }  
    We can see i have implemented abstract method Display in class P2 which inherits abstract class P1.

  8. Keyword static can not be used with abstract method.

  9. Abstract class does not support multiple inheritance.Inheritance from abstract to abstract class is possible.

  10. For example look at following code:
    1. abstract class P1  
    2. {  
    3.     public abstract void Display();  
    4.     public void Test()  
    5.     {  
    6.         Console.WriteLine("Welcome");  
    7.     }  
    8. }  
    9. abstract class P3: P1  
    10. {  
    11.     abstract public void Test1();  
    12. }  
    13. abstract class p4: P3  
    14. {  
    15.     abstract public void Test2();  
    16. }  
    17. class P2: p4  
    18. {  
    19.     public override void Display()  
    20.     {  
    21.         Console.WriteLine("Hi");  
    22.     }  
    23.     public override void Test1()  
    24.     {  
    25.         Console.WriteLine("Hello");  
    26.     }  
    27.     public override void Test2()  
    28.     {  
    29.         Console.WriteLine("Welcome");  
    30.     }  
    31. }  
  11. We can not create a object of abstract class look at the below image, it will raise an error can not create instance of an abstract class.

  12. We can not create a object of abstract class, but we can create reference of it. Below is the practical example:
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Threading.Tasks;  
    6. namespace ConsoleApplication5  
    7. {  
    8.     abstract class P1  
    9.     {  
    10.         public abstract void Display();  
    11.         public void Test()  
    12.         {  
    13.             Console.WriteLine("Welcome");  
    14.         }  
    15.     }  
    16.     abstract class P3: P1  
    17.     {  
    18.         abstract public void Test1();  
    19.     }  
    20.     abstract class p4: P3  
    21.     {  
    22.         abstract public void Test2();  
    23.     }  
    24.     class P2: p4  
    25.     {  
    26.         public override void Display()  
    27.         {  
    28.             Console.WriteLine("Hi");  
    29.         }  
    30.         public override void Test1()  
    31.         {  
    32.             Console.WriteLine("Hello");  
    33.         }  
    34.         public override void Test2()  
    35.         {  
    36.             Console.WriteLine("Welcome");  
    37.         }  
    38.     }  
    39.     class Program  
    40.     {  
    41.         static void Main(string[] args)  
    42.         {  
    43.             p4 objP4 = new P2();  
    44.             objP4.Display();  
    45.             objP4.Test();  
    46.             objP4.Test1();  
    47.             objP4.Test2();  
    48.             P2 obj3 = new P2();  
    49.             obj3.Test2();  
    50.             obj3.Test1();  
    51.             obj3.Test();  
    52.             obj3.Display();  
    53.         }  
    54.     }  
    55. }