Home
Search
abstract KeywordUnderstand abstract methods and classes. Abstract things cannot have implementations or be instantiated.
C#
This page was last reviewed on Jan 24, 2023.
Abstract. In C# a method or class can be marked as abstract. An abstract method has no implementation. Its implementation logic is provided instead by classes that derive from it.
class
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.
Example code. We introduce 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.
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(); } }
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.
abstract class Test { public abstract void A(); } class Example1 : Test { } class Program { static void Main() { } }
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.
Performance. In my testing, abstract classes with virtual methods have better performance than interface implementations in .NET. Abstract classes can help performance.
Interface
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 24, 2023 (simplify).
Home
Changes
© 2007-2023 Sam Allen.