
.NET 1.1+C# Abstract Classes
The twentieth part of the C# Object-Oriented Programming tutorial investigates the use of abstract classes. These are special classes that are designed to be used only as base classes for inheritance. They do not permit the instantiation of objects.
What is an Abstract Class?
So far in this tutorial we have considered concrete classes. A concrete class is a simple class with members such as methods and properties. The class describes the functionality of the objects that it can be used to instantiate.
Often, when working with inheritance hierarchies, the least specialised base class cannot fully represent a real object. For example, classes that represent cubes, spheres and other three-dimensional objects can all be assigned properties and methods that permit the calculation of their volume and surface area. However, if the base class for these types represents generic 3D objects, these calculations are not possible. Furthermore, it may be nonsensical to create an instance of such a class.
An abstract class provides all of the characteristics of a concrete class except that it does not permit objects of the type to be created. An abstract class merely defines members that are used for inheritance, defining the functionality of its child classes. These members may themselves be abstract, effectively declaring a placeholder that must be implemented by subclasses. Members may also be concrete, including real functionality, and may be marked as virtual to support polymorphism via method overriding.
Creating an Abstract Class
To demonstrate the use of abstract classes we will create an example project based around the "ThreeDObject" class. This abstract class will include both abstract and concrete methods and properties. We will then create two concrete subclasses of ThreeDObject to represent cubes and spheres.
To begin, create a new console application named "AbstractClassesDemo". Add a class file to the project and name it "ThreeDObject".
Syntax
To declare that a class is abstract, the "abstract" keyword is used as a prefix to the class definition. To change the ThreeDObject class to abstract, modify the standard declaration as follows:
abstract class ThreeDObject
Now that the class is abstract, it will not be possible to instantiate ThreeDObject objects. This can be shown by adding the following code to the Main method of the program. When you attempt to compile the program, an error occurs.
ThreeDObject cube = new ThreeDObject();
Creating an Abstract Property
An abstract property may only be created within an abstract class. Such a property acts as a placeholder only and contains no functionality of its own. Instead, it ensures that any non-abstract class derived from the abstract base class must implement a matching property. If the subclass is also abstract, the property does not need to be declared again unless it is being made concrete.
As with concrete properties, abstract properties are defined with get and set accessors. If the set accessor is omitted then the property will be read-only. For a write-only property, only the set accessor is included. As the abstract property can contain no code, no code block is created for either accessor.
The following code shows examples of read-write, read-only and write-only abstract properties:
public abstract int ReadWriteProperty { get; set; }
public abstract int ReadOnlyProperty { get; }
public abstract int WriteOnlyProperty { set; }
We will add an abstract property to the ThreeDObject to represent the surface area of a three-dimensional object. This read-only property is a good example of an abstract member as it is impossible to determine the surface area for the general type. However, subclasses that represent a specific type of three-dimensional object will be able to perform this calculation.
To create the surface area property, add the following code to the ThreeDObject class:
public abstract double SurfaceArea { get; }
5 April 2008