This is my first article to this booming educational hub C# Corner. This platform is amazing for learning and nurturing your skills with a huge collection of highly qualified professionals.

There is a large number of articles written for this topic. I am writing this for beginners.

Abstract Class

An Abstract class is an incomplete class with abstract members that cannot be instantiated and are either partially implemented or not at all implemented.

In other words this class does not have an implementation but only declarations of abstract methods so it does not make sense to make an instance of an incomplete class. It can have non-abstract methods with implementation.

An Abstract class can act as a base class in any project and the classes to be inherit from this base abstract class need to provide an implementation for the abstracts methods of the abstract base class.

An Abstract class can be created using the "abstract" keyword as in the following:

Public Abstract Class Employees

{

// provide Declaration of a abstract methods

Public abstract void Getdata();

// Non Abstract methods

Public void displayData()

{

//Code Please

}

}

When a class is inherited from a base abstract class it has two options.

Either the derived class must implement all the abstract methods or the derived class should be an abstract class.

Let us see what that means.

Taking an example of a customers class having three methods, two abstract and one non-abstract. The following abstract methods only have a declaration provided but for the non-abstract method Add() you need to provide an implementation.

public abstract class customers

{

public customers()

{

//can initialization code be provided.

}

public abstract void display();

public abstract void Print();

public void Add()

{

// Add Code here

}

}

When this abstract class is being inherited by a derived class.

Here the program class has been inherited from the customers class and we need to provide an implementation for the abstract methods display() and Print().

class Program : customers

{

public override void display()

{

Console.WriteLine("Customer Data displayed");

}

public override void Print()

{

Console.WriteLine("Customer Data Printed");

}

static void Main(string[] args)

{

// Program p = new Program();

customers c = new Program();

c.display();

Console.ReadLine();

}

}

If you don't want to provide an implementation for the abstract methods from the base abstract class then the Derived class should be abstract.

abstract class Program : customers

{

static void Main(string[] args)

{

// Program p = new Program();

// customers c = new Program();

// c.display();

Console.ReadLine();

}

}

Important Sticky

Where and why abstract should be used.

This is very important to understand why and where abstract classes should be used.

The following is some of the reasons.