Chapter 13. Interfaces

   

At times a designer does not want to create a new type. Rather, the designer wants to describe a set of behaviors that any number of types might implement. For example, a designer might want to describe what it means to be storable (i.e., capable of being written to disk or to a database) or printable.

Such a description is called an interface. An interface is a contract: the designer of the interface says "if you want to provide this capability, you must implement these methods ." The implementer of the interface agrees to the contract and implements the required methods.

When a class implements an interface, it tells any potential client "I guarantee I'll support the methods, properties, events, and indexers of the named interface." The interface details the return type from each method and the parameters to the methods.

See Chapter 9 for information about methods and properties; see Chapter 18 for information about events; and see Chapter 14 for coverage of indexers.

When specifying interfaces, it is easy to get confused about who is responsible for what. There are three concepts to keep clear:

The interface

This is the contract. By convention, interface names begin with a capital I; thus, your interface might have a name like IPrintable. The IPrintable interface might describe a Print( ) method.

The implementing class

This is the class that agrees to the contract described by the interface. For example, Document might be a class that implements IPrintable and thus implements the Print( ) method.

The client class

This is a class that calls methods from the implementing class. For example, you might have an Editor class that calls the Document class's Print( ) method.

Interfaces Versus Abstract Base Classes

Programmers learning VB.NET often ask about the difference between an interface and an abstract ( MustInherit ) base class. The key difference is subtle: an abstract base class serves as the base class for a family of derived classes, while an interface is meant to be mixed in with other inheritance trees.

Inheriting from an abstract class implements the is-a relationship, introduced in Chapter 11. Implementing an interface defines a different relationship, one we've not seen until now: the implements relationship. These two relationships are subtly different. A car is a vehicle, but it might implement the CanBeBoughtWithABigLoan capability (as can a house, for example).

Interfaces are a critical addition to any framework, and they are used extensively throughout .NET. For example, the collection classes (array lists, stacks, and queues) are defined, in large measure, by the interfaces they implement. (The collection classes are explained in detail in Chapter 15).

In this chapter, you will learn how to create, implement, and use interfaces. You'll learn how one class can implement multiple interfaces. You will also learn how to make new interfaces by combining existing interfaces or by extending (deriving from) an existing interface. Finally, you will learn how to test whether a class has implemented an interface.

   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net