About Abstract Class

The class which is declared using abstract keyword is an abstract class. An abstract class cannot be initialized it can be only be inherited by another class. Abstract consist of abstract methods and consists of an instance method.

What is an Abstract class?

The class which is declared using abstract keyword is an abstract class. An abstract class cannot be initialized, it can only be inherited by another class. Abstract consists of abstract methods and can also consist of an instance method. Only methods, properties ,and indexers can be abstract, fields cannot be abstract.

Constructor calls the sequence when any class derived from abstract class is initialized and is the same as the normal class as shown below,

  1. Static constructor of the base class is called.
  2. Static constructor of the derived class is called.
  3. Instance constructor base class is called.
  4. Instance constructor derived class is called.
The class which is inheriting abstract class must provide the implementation of abstract properties, indexers and methods.
 
Example of abstract properties,
  1. abstract class Employee {  
  2.     public abstract string firstName {  
  3.         get;  
  4.         set;  
  5.     }  
  6. }  
  7. class Manager: Employee {  
  8.     private string _firstName;  
  9.     public override string firstName {  
  10.         set {  
  11.             this._firstName = value;  
  12.         }  
  13.         get {  
  14.             return this._firstName;  
  15.         }  
  16.     }  
  17. }  
Inheritance Chaining and Abstract class
 
In inheritance chaining, if there are three classes and they consist of methods which override at every level of inheritance it looks as shown below,
 
Part 1 
  1. class A {  
  2.     public virtual void Method1() {  
  3.         Console.WriteLine("Class A Method1");  
  4.     }  
  5. }  
  6. class B: A {  
  7.     public override void Method1() {  
  8.         Console.WriteLine("Class B Method1");  
  9.     }  
  10. }  
  11. class C: B {  
  12.     public override void Method1() {  
  13.         Console.WriteLine("Class C Method1");  
  14.     }  
  15. }  
If we want class C to not override the implementation of B but provide new implementation of Method1 we can achive this using abstract class.
 
Part 2
  1. class A {  
  2.     public virtual void Method1() {  
  3.         Console.WriteLine("Class A Method1");  
  4.     }  
  5. }  
  6. class B: A {  
  7.     public override void Method1() {  
  8.         Console.WriteLine("Class B Method1");  
  9.     }  
  10. }  
  11. abstract class D: B {  
  12.     public abstract new void Method1();  
  13. }  
  14. class C: D {  
  15.     public override void Method1() {  
  16.         Console.WriteLine("Class C Method1");  
  17.     }  
  18. }  
Part 3
  1. class A {  
  2.     public virtual void Method1() {  
  3.         Console.WriteLine("Class A Method1");  
  4.     }  
  5. }  
  6. class B: A {  
  7.     public override void Method1() {  
  8.         Console.WriteLine("Class B Method1");  
  9.     }  
  10. }  
  11. abstract class D: B {  
  12.     public abstract override void Method1();  
  13. }  
  14. class C: D {  
  15.     public override void Method1() {  
  16.         Console.WriteLine("Class C Method1");  
  17.     }  
  18. }  
The behavior of part 1 and part 3 are the same.
 
Important Points to be noted about abstract class,
  1. Abstract keyword over method is implicitly virtual.
  2. We cannot use the virtual keyword with the abstract.
  3. We cannot use abstract with static because the abstract method is meant to be overridden.