HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

Abstract Classes in C#
Definition: Abstract classes are incomplete classes and it is a compile time error to create an instance of an abstract class. Abstract classes can only be used as base classes. 

Let us understand abstract classes and related key concepts with an example. In the example below
  • "AbstractClass" is an abstract class as the class is prefixed with the abstract keyword.
  • Abstract classes may or may not contain abstract members. In our example "AbstractClass" contains one concrete method and one abstract method. Display() is a concrete method where as ExampleMethod() is an abstract method.
  • If a class contains an abstract method, the class must be marked as abstract. It is compile time error to have an abstract method inside a non abstract class.
  • Abstract methods should not have body, only the method signature as shown in our example below. Abstract methods should be declared as public. It is a compile time error if you don't declare an abstract method as public or use private access modifier for an abstract method. Declaring abstract ExampleMethod() as shown below would generate a compile time error.
    • private abstract void ExampleMethod();
    • abstract void ExampleMethod();
  • Abstract classes can only be used as base classes. In our example AbstractClass is the base class for MainClass. It is a compile time error to use both sealed and abstract keywords on a class declaration. This is because sealed classes cannot be used as base classes and abstract classes can be used only as base classes. So a class cannot be sealed and abstract at the same time. Declaring the ExampleMethod() as shown below is a compile time error.
    • public sealed abstract void ExampleMethod();
  • Any class that inherits from an abstract class will inherit all the abstract members. The class must either implement all the inherited abstract members or should be marked as abstract. In our example MainClass inherits the abstract ExampleMethod(). MainClass has provided the implementation for the ExampleMethod(). Hence we need not mark the MainClass as abstract. If the MainClass did not provide implementation, the class should be marked as abstract.
  • Abstract methods in C# are implicitly virtual. It is a compile time error to mark an abstract method as virtual. Declaring the ExampleMethod() as shown below is a compile time error.
    • public virtual abstract void ExampleMethod();

using System;

abstract class AbstractClass

{

    public void Display()

    {

        Console.WriteLine("Hello PRAGIM Technologies");

    }

    //Abstract method, no body only declaration

    public abstract void ExampleMethod();

}

class MainClass : AbstractClass

{

    //MainClass implementing inherited abstract method

    public override void ExampleMethod()

    {

        Console.WriteLine("ExampleMethod Implemented");

    }

    public static void Main()

    {

        //Error! Cannot create instance of an abstract class

        //AbstractClass AC = new AbstractClass();

        MainClass MC = new MainClass();

        MC.Display();

        MC.ExampleMethod();

    }

}

Partially implementing abstract classes
In the example below
  • Class1 is an abstract class and has 2 abstract methods Method1() and Method2().
  • Class2 is inheriting from Class1. Class2 has to provide implementation for both the abstract methods Method1() and Method2(), but class2 is providing implementation only for Method1() and Method2(). Class2 is partially implementing abstract Class1. Hence Class2 is also marked as abstract class.
  • As both Class1 and Class2 are abstract classes we cannot create instance of any class.
  • The MainClass is inheriting from Class2 and providing implementation for Method2(). Class1 has already implemented Method1(). So MainClass can be instantiated and we can call Method1() and Method2() as shown in the example below.

using System;

abstract class Class1

{

    public abstract void Method1();

    public abstract void Method2();

}

 

abstract class Class2:Class1

{

    public override void Method1()

    {

        Console.WriteLine("Method1 Implemented");

    }

}

class MainClass : Class2

{

    //MainClass implementing inherited abstract method

    public override void Method2()

    {

        Console.WriteLine("Method2 Implemented");

    }

    public static void Main()

    {

        MainClass MC = new MainClass();

        MC.Method1();

        MC.Method2();

    }

}

Further Reading
Other related articles

Njoy Programming
   ByPrasad Cherukuri