The abstract modifier indicates that the thing being modified has a missing or incomplete implementation. The abstract modifier can be used with classes, methods, properties, indexers, and events. In this article, I will provide you details of using abstract keyword on class and methods in C# using working console application example.

C# Abstract Class

Using the abstract modifier in a class declaration indicates that a class is intended only to be a base class of other classes, i.e, Abstract class in csharp is defined using "abstract" keyword.

Syntax of abstract class:

public abstract class A
{
    // Class members here.
}

The class which cannot be instantiated known as abstract class.Abstract class has no object.

The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share

Abstract class can contain abstract and non-abstract members.

Abstract member has only declaration and non-abstract member has declaration and implementation in abstract class. Abstract class only allow other classes to inherit from it and abstract members must be implemented by classes that derive from the abstract class.

The advantage of abstract class is that it enforces certain hierarchies for all the subclasses. Class declare as abstract class by putting the abstract keyword before the class keyword in the class definition.

Properties of abstract class:

  1. Abstract class can contain only abstract members or only non-abstract members or both.
  2. Abstract class cannot be instantiated.
  3. Abstract method cannot be static.
  4. Abstract method cannot be virtual.
  5. Abstract method cannot be private.
  6. A class cannot be inherit more than one abstract class.
  7. Abstract class can contain properties.
  8. Abstract method can be overloaded.

Example of Abstract class

using System;


namespace AbstractClass
{
    abstract class ShapesClass
    {
        abstract public int Area();
    }
    class Square : ShapesClass
    {
        int side = 0;

        public Square(int n)
        {
            side = n;
        }
        // Area method is required to avoid
        // a compile-time error.
        public override int Area()
        {
            return side * side;
        }

        static void Main()
        {
            Square sq = new Square(12);
            Console.WriteLine("Area of the square = {0}", sq.Area());
            Console.ReadLine();
        }

        interface I
        {
            void M();
        }
        abstract class C : I
        {
            public abstract void M();
        }

    }
}

c-sharp-abstract-class-min.png

Let's take a look on another example, below a simple example of abstract class Test which have one abstract method Multiply and one non-abstract method Square. This class is inherited by Program class and it implemented abstract method using override keyword, override keyword indicate that this method declared somewhere. For accessing to methods of abstract class we create a object of Program class instead of abstract class because abstract class does not allow to create their object anywhere.

using System;
namespace Abstraction
{
    abstract class Test
    {
        public int Square(int a)
        {
            return a * a;
        }
        public abstract int Multiply(int a, int b);
    }
    class Program : Test
    {
        static void Main(string[] args)
        {
            Program pro = new Program();
            int square = pro.Square(2);
            int multiply = pro.Multiply(5, 4);
            Console.WriteLine("Square - " + square);
            Console.WriteLine("Multiply - " + multiply);
        }
        public override int Multiply(int x, int y)
        {
            return x * y;
        }
    }
}

1. Abstract class can contain only abstract method or only non-abstract method or both.

abstract class Test {
 public int Square(int a) {
  return a * a;
 }
}
abstract class Test {
 public abstract int Multiply(int a, int b);
}
abstract class Test {
 public int Square(int a) {
  return a * a;
 }
 public abstract int Multiply(int a, int b);
}

2. Abstract class has no object means an abstract class cannot be instantiated.

class Program: Test {
 static void Main(string[] args) {
  Test test = new Test();
 }
}

3. Abstract method cannot be declare as static.

public abstract static int Multiply(int a,int b);
// It will throw an exception, like:
// A static member 'Test.Multiply(int, int)' cannot be marked as override, virtual, or abstract

4. Abstract method can not be declare as virtual.

public abstract virtual int Multiply(int a,int b);
// It will throw an exception, like:
// The abstract method 'Test.Multiply(int, int)' cannot be marked virtual

5. Abstract class can not be declare as private.

private abstract int Multiply(int a,int b);
// It will throw an exception, like:
// 'Test.Multiply(int, int)': virtual or abstract members cannot be private

6. A class cannot inherit more than one abstract class.

abstract class Test {}
abstract class Test1 {}

// It will throw an exception
//Error 'Program' cannot have multiple base classes: 'Test' and 'Test1'
class Program: Test, Test1 {}

7. An abstract class may contain property or accessor

abstract class Test
{
        // Define property
        public string message { get; set; }
        // Abstract method
        public abstract void DisplayMessage();
}

class Program : Test
{      
        static void Main(string[] args)
        {          
            Program pro = new Program();
            // Access property
            pro.message = "Hello qawithexperts";
            // Access abstract method
            pro.DisplayMessage();         
            Console.Read();
        }

        // Implement abstract method in derived class
        public override void DisplayMessage()
        {           
            // Use property from abstract class
            Console.WriteLine(message);
        }
}

// Output
// Hello qawithexperts

8. Abstract method can be overloaded. You can define number of overloaded method in abstract class, but make sure that all overloaded method must be implemented in drive class, otherwise it will throw exception.

abstract class Test
{
        // Abstract method
        public abstract int Multiply(int a, int b);
 
        // Abstract method overload
        public abstract int Multiply(int a, int b, int c);

}

class Program : Test
{      
        static void Main(string[] args)
        {          
            
            Program pro = new Program();

            // Access abstract method
            int i = pro.Multiply(2, 3);
            // Access abstract method
            int j = pro.Multiply(4, 5, 6);
 
            Console.WriteLine("First method Multiply - " + i);
            Console.WriteLine("Second method Multiply - " + j);
            Console.Read();
}


       // Implement abstract method
        public override int Multiply(int a, int b)
        {
            return a * b;
        }

        // Implement overload abstract method
        public override int Multiply(int a, int b, int c)
        {
            return a * b * c;
        }       

}

// Output
// First method Multiply - 6
// Second method Multiply - 120

If a class inherit an abstract class then need to implement all abstract method otherwise it will throw exception

// Exception       

//'Abstraction.Program' does not implement inherited abstract member 'Abstraction.Test.Multiply(int, int)'

abstract class Test {
 public abstract int Multiply(int a, int b);
}

class Program: Test {
 static void Main(string[] args) {}
}