
Abstract methods have no implementations. The implementation logic is provided rather by classes that derive from them. We use an abstract class to create a base template for derived classes. Certain restrictions, negatives and positives are found in abstract types.
KeywordsThis C# tutorial covers the abstract keyword. Abstract methods cannot have implementations. Abstract classes cannot be instantiated.
We introduce first an abstract class named Test. Two other classes derive from Test: the Example1 and Example2 classes. In the Test class, we have a field and also an abstract method. Abstract methods cannot have bodies because those bodies would never be used.
Program that uses abstract class [C#]
using System;
abstract class Test
{
public int _a;
public abstract void A();
}
class Example1 : Test
{
public override void A()
{
Console.WriteLine("Example1.A");
base._a++;
}
}
class Example2 : Test
{
public override void A()
{
Console.WriteLine("Example2.A");
base._a--;
}
}
class Program
{
static void Main()
{
// Reference Example1 through Test type.
Test test1 = new Example1();
test1.A();
// Reference Example2 through Test type.
Test test2 = new Example2();
test2.A();
}
}
Output
Example1.A
Example2.ADerived classes. When you create a derived class like Example1 or Example2, you must provide an override method for all abstract methods in the abstract class. The A() method in both derived classes satisfies this requirement.

Int field. An abstract class can have an instance field in it. The derived classes can access this field through the base syntax. This is a key difference between abstract classes and interfaces.
Cannot instantiate abstract class. The important part of an abstract class is that you can never use it separately from a derived class. Therefore, in Main(), you cannot use the new Test() constructor. However, you can use the Test type directly once you have assigned it to a derived type such as Example1 or Example2.

What is the difference between an abstract class and an interface? An abstract class can have fields on it; these fields can be referenced through the derived classes. An interface, on the other hand, cannot have fields. Essentially, an abstract class is the same thing as an interface except it is an actual class, not just a contract.
Interface Types
In my testing, abstract classes with virtual methods have better performance than interface implementation in the .NET Framework 4.0. The benchmark in the linked article remains the same if you use an abstract class instead of a regular class.
Interface Versus Virtual Method Performance
An abstract class is one that cannot be directly instantiated. Instead derived classes must inherit from it and their constructors must be used. It provides some additional functionality to interfaces and also has better method performance. Abstract classes use the abstract keyword; abstract methods use a special form where the method body is replaced by a semicolon.
Class Examples