Abstract classes are classes that always serves as base class for other classes. Abstract classes are slightly similar to interfaces but abstract classes can also have methods and properties that already have implementations. Abstract classes can also have constructors just like a normal class. You cannot instantiate abstract classes because abstract classes main purpose is to serve as the base class for derived classes so the instantiation will be handled by the derived classes. The following is an example of using an abstract class.

sing System;
 
namespace AbstractClassDemo
{
    public abstract class Base
    {
        protected int number;
        protected string name;
 
        public abstract int Number
        {
            get;
            set;
        }
 
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
 
        public abstract void ShowMessage();
 
        public Base(int number, string name)
        {
            this.number = number;
            this.name = name;
        }
    }
 
    public class Derived : Base
    {
        public override void ShowMessage()
        {
            Console.WriteLine("Hello World!");
        }
 
        public override int Number
        {
            get
            {
                return number;
            }
            set
            {
                number = value;
            }
        }
 
        public Derived(int number, string name)
            : base(number, name)
        {
        }
    }
}

Example 1 – Using an Abstract Class

To declare a class as abstract, just use the abstract keyword. Inside our abstract class, we defined two protected fields that will be used by our properties. One of our properties is also abstract. Notice that it also uses the abstract keyword. This property needs to be overridden by the classes that will inherit from this class. Since that property is abstract, the getters and setters should not have a body. You can see that abstract classes can also contain normal properties as demonstrated by the Name property. We defined an abstract method. Again, the abstract keyword was used and the deriving class should override this method. The abstract class has its own constructor. Abstract classes should have at least one member that is abstract.

We defined another class that inherits from the Base class. We have overridden the abstract property and method by using the overrides keyword. We also defined a constructor and use the base keyword to pass the value of the parameters to the base constructor. Again, you cannot create abstract classes, but since we made a class that inherits from it, you can use that derived class instead.