C# abstract Keyword

Understand abstract methods and classes. Abstract things cannot have implementations or be instantiated.

Abstract. A method can be abstract. A class can be abstract. An abstract method has no implementation. Its implementation logic is provided instead by classes that derive from it.ClassKeywords

A template. We use an abstract class to create a base template for derived classes. With abstract, we enforce a design rule at the level of the compiler.

An intro. 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.
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.
Note: When we derive a class like Example1 or Example2, we must provide override methods for all abstract methods in the abstract class.
Tip: In this program, the A() method in both derived classes satisfies this requirement.
C# program that uses abstract class 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

Error, abstract member. We must provide an "override" method for abstract methods. Here Example1 does not implement the "A" method and the compiler does not like this.
C# program that causes compilation error abstract class Test { public abstract void A(); } class Example1 : Test { } class Program { static void Main() { } } Output error CS0534: 'Example1' does not implement inherited abstract member 'Test.A()'

Notes, field. An abstract class can have an instance field in it. The derived classes can access this field through "base." This is a key difference between abstract classes and interfaces.Base

Notes, instantiation. The important part of an abstract class is that you can never use it separately from a derived class. It can only be used through another class.

A discussion. 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.
And: An interface cannot have fields. An abstract class is the same thing as an interface except it is a class, not just a contract.

Static class. A static class cannot be abstract. Static classes cannot be instantiated or derived from. This makes the abstract keyword have no meaning for them.Static

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

Design. Often programs that use abstract classes are designed better. Less code duplication occurs. With less code, we tend to have higher-quality and better-tested code.
So: In solving the problem of code duplication among many object types, abstract classes help improve code quality and performance.

A summary. An abstract class is not directly instantiated. Instead, derived classes must inherit from it. Compared to an interface, it adds features and improves performance.

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.