Abstract Classes in C#

An abstract class is an incomplete class because it does not have an implementation for all of its members since they are abstract and an abstract class cannot be sealed.

We all know how to create a class.

To create a class we do the following:

  1. public class NonAbstractClass {  
  2.   
  3. }  
The preceding class is a non-abstract class and to create an abstract class, we use an abstract keyword before the class keyword.
  1. public abstract class MyAbstractClass {  
  2.   
  3. }  
Within a class, any member can become an abstract member, for example, properties, events, methods and so on. And how can we make the members abstract?

Using an abstract keyword

Let's look at an example where I want a method that is an abstract method.
  1. public abstract class MyAbstractClass {  
  2.    public void MyAbstractMethod() {  
  3.   
  4.    }  
  5. }  
Currently the preceding, MyAbstractMethod is a non-abstract method and to make it abstract, all we need to do is use an abstract keyword.
  1. public abstract class MyAbstractClass {  
  2.    public abstract void MyAbstractMethod() {  
  3.   
  4.    }  
  5. }  
But when we build our solution, we get a compile time error stating we cannot declare a body because it is marked abstract.

error

The reason is, we can only provide an implementation for the abstract members in a class that inherits the abstract class.
  1. public abstract class MyAbstractClass {  
  2.    public abstract void MyAbstractMethod();   
  3. }  
So now, we have an abstract class with an abstract member.

Instance of an abstract class

I told you earlier that abstract classes are incomplete because they do not have an implementation for all of its members since they are abstract and since they are abstract, it doesn't make any sense to be able to create an instance of this class and use those members to do something. That's why you cannot create an instance of an abstract class and they can only be used as a base class for other classes.

In my project, I have another class named “Program” that is a non-abstract class. Now what we will do is, we will inherit our Program class from the abstract class that we have created, in other words MyAbstractClass.

If you look at this program class, it is a non-abstract class because we haven't used an abstract keyword.
  1. public class Program {  
  2.    static void Main(string[] args) {  
  3.    }  
  4. }  
When a non-abstract class inherits from an abstract class, then a non-abstract class must provide an implementation for all the abstract members of an abstract/base class.
  1. public class Program : MyAbstractClass{  
  2.    static void Main(string[] args) {  
  3.    }  
  4. }  
If we try to compile our project we will now get a compile-time error stating that the Program class does not implement an inherited abstract member that is present in the abstract/base class.

abstract

If your program class doesn't wish to provide an implementation, then you can mark your program class as an abstract class.
  1. public abstract class Program : MyAbstractClass{  
  2.    static void Main(string[] args) {  
  3.    }  
  4. }  
Now if you build the solution, you won't get an error.

But on the other hand, if you don't want your Program class to be marked as abstract then provide an implementation for all the abstract members and provide an implementation for the abstract members. Use an override keyword for that.
  1. class Program : MyAbstractClass{  
  2.    override space abstractMethod press enter  
abstractMethod
  1. class Program : MyAbstractClass{  
  2.    public override void MyAbstractMethod() {  
  3.    throw new NotImplementedException();  
  4.    }  
  5. }  
Remove the default implementation and provide your own.
  1. class Program : MyAbstractClass {  
  2.    public override void MyAbstractMethod() {  
  3.    Console.WriteLine("my abstract method implementation");  
  4.    }  
  5. }  
To print the implementation on the console screen, we need to create an instance of our Program class that is a non-abstract class.
  1. class Program : MyAbstractClass {  
  2.    public override void MyAbstractMethod() {  
  3.    Console.WriteLine("my abstract method implementation");  
  4.    }  
  5.    static void Main(string[] args) {  
  6.    Program p = new Program();  
  7.    p.MyAbstractMethod();  
  8.    }  
  9. }  
console

You can do the same by using a base reference variable pointing to your child class object.
  1. static void Main(string[] args) {  
  2.    MyAbstractClass myAbstract = new Program();  
  3.    myAbstract.MyAbstractMethod();  
  4. }  
The output will be the same.

Output

An abstract class cannot be sealed.
We have learned, if you want to prevent your class from being inherited by another class then we use a sealed keyword. But if we go by the definition, an abstract class can only be used as a base class and so it cannot be sealed.
  1. abstract sealed class MyAbstractClass {  
  2.   
  3. }  
Build your program.

Build your program

An abstract class may contain abstract members but not mandatory.

Here I have an abstract class MyAbstractClass with an abstract member MyAbstractMethod but I can also have a member in the same abstract class MyNonAbstractMethod that is a non-abstract member.
  1. abstract class MyAbstractClass {  
  2.    public abstract void MyAbstractMethod();  
  3.    public void MyNonAbstractMethod() {  
  4.    Console.WriteLine("my non-abstract method implementation");  
  5.    }  
  6. }  
If we build our program, we will not get an error.

Important Note

If a class inherits an abstract class, provide an implementation for all the abstract members inherited from the base abstract class.

If a class does not wish to provide an implementation for all the abstract members inherited from the abstract base class, then the class must be marked as abstract.