Interfaces

Interfaces

An interface in C# is similar to a pure abstract class in C++. An interface is similar to a class, but it has no member variables and no implementation of any interface methods. An interface is simply a description of work that a derived class can perform.

Using Interfaces

An interface can never be instantiated.

// Won't work; can't be instantiated directly.
ISpeak speaker = new ISpeak();

You must create a class that’s derived from the interface and create an instance of the class, as in the following example:

class Cat: ISpeak
{
    
    

}

Cat anastasia = new Cat();

Likewise, it’s incorrect to say that you have an ISpeak object—it’s more accurate to say that you have an object that implements the ISpeak interface.

Classes that implement an interface are responsible for implementing all methods that are part of the interface. If one or more methods in an interface aren’t implemented, the class can’t be instantiated, as shown here:

interface ISpeak
{
    void StringToSpeech(string Message);
    void TextFileToSpeech(string FileName);
}

class SpeechEngine: ISpeak
{
    public void StringToSpeech(string Message)
    {
        
    

    }

    // TextFileToSpeech not implemented.
}

In this example, the SpeechEngine class doesn’t implement the TextFileToSpeech method inherited from the ISpeak interface. The C# compiler will flag this as an error because a non-abstract class that inherits from an interface must provide an implementation for all members. If SpeechEngine were declared as abstract, it could declare the nonimplemented member method as abstract, and the compiler would be happy; by declaring the class and the nonimplemented method as abstract, the intent to defer implementation has been made clear.

Classes and Interfaces

So far, we’ve examined how classes are created in C# and we’ve looked at inheritance. As noted in the previous section, the C# language doesn’t support inheritance from multiple base classes, but it does support implementing multiple interfaces. In some cases, implementing multiple interfaces can enable your classes to have some of the benefits of multiple inheritance.

In the .NET Framework, you often have a choice of either implementing an interface or deriving directly from a base class. For example, when creating a new collection class, you can either derive from the CollectionBase class, which implements the IList, ICollection, and IEnumerable interfaces, or you can implement those interfaces directly. Classes that implement these three interfaces can be used like any of the basic collection classes offered by the .NET Framework.

In many cases, it makes sense to take advantage of the functionality of the base class provided by the .NET Framework. However, if your new class is so specialized that it can’t take advantage of the functionality offered by the base class, it might make sense for you to spend the time required to implement the interfaces in your class.



Part III: Programming Windows Forms