An abstract class is a lot like an interface, except that an abstract class actually contains some code. Abstract classes may not be directly instantiated. They're designed to be used only as a base class to other classes. Listing 2.17 shows an example of an abstract class and another class that inherits it.
C#
abstract class ABaseClass { public abstract void MyMethod() { // base method implementation } } public class SuperClass : ABaseClass { public override void MyMethod() { // override implementation } }
VB.NET
MustInherit Class ABaseClass Public MustOverride Sub MyMethod() ' base method implementation End Sub End Class Public Class SuperClass Inherits ABaseClass Public Overrides Sub MyMethod() ' override implementation End Sub End Class
An example of an abstract class in the .NET Framework is the Stream class. The Stream class by itself can't be instantiated and used because it's not complete enough. Instead, it acts as the base class for a number of other classes, including NetworkStream and MemoryStream. The base Stream class has a number of useful members that are common to all of the derived classes, but the base members aren't useful without the specific implementation found in the derived classes. Check the documentation in the .NET Framework SDK for more information on these classes.