C# Abstract

Abstract

An abstract method has no implementation. Its implementation logic is provided instead by classes that derive from it. We use an abstract class to create a base template for derived classes.

Keywords

Example

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 an abstract method.

Program

Methods:Abstract methods cannot have bodies. This makes sense: these bodies would never be used.

Classes:Abstract classes have certain restrictions. They cannot be constructed directly.

Tip:These are features.
They enforce a design concept.
An abstract type is, by definition, not a concrete one.

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.A
Squares

Derived 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.

Override

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.

Int

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.

Discussion

Question

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.

Interface

Thus:An abstract class is essentially the same thing as an interface except it is an actual class, not just a contract.

Note

A static class cannot be abstract. Static classes cannot be instantiated or derived from. This makes the abstract keyword have no meaning for them. Abstract classes have effect only when used with inheritance.

Static:A static class is a class with additional restrictions placed upon it. With "static" on a class, we take away features.

Static Class

Performance

Performance optimization

In my testing, abstract classes with virtual methods have better performance than interface implementation in the .NET Framework 4.0. We show that performance remains the same with an abstract class instead of a regular class.

Interface Performance

In my view, programs that use abstract classes tend to be better-designed. Less code duplication occurs in them.
With less code,
we tend to have higher-quality
and better-tested code. This leads to faster and more reliable programs.

So:In solving the problem of code duplication among many object types, abstract classes help improve code quality and performance.

Summary

C# language

An abstract class is one that cannot be directly instantiated. Instead, derived classes must inherit from it—their constructors must be used. Compared to an interface, it adds features and improves performance.

Review:Abstract classes use the abstract keyword.
Abstract methods use a special form.
The method body is replaced by a semicolon.


C#: Class