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# Interfaces

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 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 IPrey 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's 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.

Implementing Interface Members

Members of implemented interfaces can be declared in two manners. The first is known as implicit implementation. This simply means that each interface member required is declared in the class using the same signature as in the interface. This is the manner that we shall use for the Cat class.

All members of an interface are publicly accessible. This behaviour cannot be modified and must be repeated in the implementing class by explicitly adding the "public" access modifier to all of the declarations.

To implement the interface members, and to create a Cat-specific "Purr" method, modify the code of the class as follows:

class Cat : IPredator
{
    private int _attackSpeed;

    public int AttackSpeed
    {
        get { return _attackSpeed; }
        set { _attackSpeed = value; }
    }

    public void Attack(IPrey prey)
    {
        if (_attackSpeed > prey.FleeSpeed)
            Console.WriteLine("Caught prey");
        else
            Console.WriteLine("Prey escaped");
    }

    public void Purr()
    {
        Console.WriteLine("Cat purred");
    }
}

Note the type of the Attack method's parameter. The prey object may be of any class that implements IPrey. As the FleeSpeed property is part of the IPrey interface, the value can be read and compared to the cat's attack speed to determine if the feline catches its prey.

A similar class can be created demonstrate the IPrey interface. This class will represent fish. Add a new class file named "Fish" to the project and copy in the following code:

class Fish : IPrey
{
    private int _fleeSpeed;

    public int FleeSpeed
    {
        get { return _fleeSpeed; }
        set { _fleeSpeed = value; }
    }

    public void Flee()
    {
        Console.WriteLine("Fish fleeing");
    }
}
15 April 2008