BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.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; }

Creating an Abstract Method

Abstract methods may only be created within abstract classes. Like abstract properties, they are a placeholder for methods that must be implemented in subclasses. To create an abstract method, the "abstract" keyword is used as a prefix to the declaration and no code block is created.

To create an abstract method in ThreeDObject that calculates the object's volume, add the following code to the class:

public abstract double CalculateVolume();

NB: It would be unusual to use a property for calculating the surface area and a method for the volume. This selection has been made for demonstration purposes only.

Creating Concrete Members in an Abstract Class

Although it is not possible to instantiate an abstract class, it is possible to create concrete members in the class. These members will be inherited by subclasses in exactly the same manner as when inheriting from concrete classes. The members may also be overridden using polymorphism if marked as virtual.

In the ThreeDObject object we will create a single concrete property and a concrete method. The property will hold the height of the three-dimensional object and the method will calculate the ratio between the object's volume and surface area. This calculation can be performed in the abstract class using the SurfaceArea property and CalculateVolume method, each of which will be defined in any subclass.

Add the property, backing store variable and method to the ThreeDObject using the following code:

private double _height;

public double Height
{
    get { return _height; }
    set { _height = value; }
}

public double CalculateVolumeToAreaRatio()
{
    return CalculateVolume() / SurfaceArea;
}
5 April 2008