BlackWaspTM
C# Programming
.NET 1.1+

C# Interfaces (2)

The twenty-first part of the C# Object-Oriented Programming tutorial completes the investigation of polymorphism in the C# programming language. This instalment describes the use of interfaces to determine required members in classes that implement them.

Creating an Interface Property

When a property is added to an interface it acts as a placeholder only, containing no functionality of its own. The functionality is added by the individual classes that implement the interface. Interface property declarations are therefore comparable to abstract property declarations and use a similar get and set accessor syntax. However, as all members of interfaces are public by definition, the "public" keyword is omitted.

The following code shows examples of read-write, read-only and write-only interface properties:

int ReadWriteProperty { get; set; }
int ReadOnlyProperty { get; }
int WriteOnlyProperty { set; }

The IPrey interface will define a property to hold the fleeing speed of prey animals. This is an important value when an animal must run away from a predator. To create the property, add the following code to the IPrey interface:

int FleeSpeed { get; set; }

Creating an Interface Method

Methods may be created within interfaces. As with properties, the method must not include an access specifier or a code block. Otherwise, the declaration uses the same syntax as a standard method.

The IPrey interface will include a method called when the animal attempts to escape from a predator. This method will be named "Flee" and will require no parameters. To create it, add the following code to the interface:

void Flee();

Creating the IPredator Interface

The IPrey interface is now complete. We will also create a second interface to represent predatory animals. This interface will define a property and a method. The property will hold the attack speed of all animals that implement the IPredator interface.

The interface's method, "Attack" will be called when the predator wants to attack another creature. This method will accept a single parameter containing the prey animal to be attacked. So that any prey animal may be attacked, the parameter will be of the IPrey type. This use of polymorphism means that an object of any class that implements IPredator will be a valid parameter value.

To create the second interface, create a new interface file named IPredator and add the following code:

interface IPredator
{
    int AttackSpeed { get; set; }

    void Attack(IPrey prey);
}

Implementing an Interface

Once an interface has been declared, one or more classes may implement it. Each class that implements an interface must make concrete all of the interface members. This is achieved by simply declaring all of the methods, properties, indexers, etc. that have been defined in the interface. In each case, the signature of the item from the interface must be exactly matched within the class implementing it.

To create a class that implements an interface, the syntax is similar to that of inheritance. The new class named is followed by the name of the implemented interface. The two names are separated with a colon character (:). Where a class is also inheriting from a base class, the parent class' name follows the colon. The implemented interface name is then appended, separated from the base class name using a comma.

In our example, we will create a class for cats. The class will implement the IPredator interface. To create the class, add a new class file to the project and name it "Cat". Modify the class definition to use the interface as follows:

class Cat : IPredator
{
}

As the Cat class does not implement all of the members of the IPredator interface, the code is not currently valid. Attempts to compile the program will produce two errors highlighting the missing method and property.

15 April 2008