Chapter 4. Interface Inheritance

An interface specifies a set of abstract methods and properties. Like an abstract base class, an interface is made concrete by each of its derived classes. Unlike an abstract base class, an interface cannot provide a default implementation or define state, such as an instance data member or constant.

An abstract base class provides a common interface for a family of related types. In computer graphics, for example, Light may serve as an abstract base class for a family of lights. Users are likely to manipulate the lights in a scene independently of the actual type—turning them on or off, repositioning them, changing their color and intensity, and so on. The delivery of a Light hierarchy includes the abstract base class and several common derived instances, such as classes representing a spotlight, a directional light, and a point light (think of the sun). We can either make use of these prebuilt instances or derive a new instance(s). For example, in the Disney film Dinosaur we introduced a class to represent a barn-door light.

An interface, on the other hand, provides a common service or characteristic for otherwise unrelated types. For example, any type wishing to be the target of the foreach statement must implement the IEnumerable interface.[1] For example, the ArrayList, BindingManager, DataView, and BitArray classes are all derived from the IEnumerable interface, but otherwise they have little in common.

[1] By convention, interfaces under .NET begin with a capital I followed by a mnemonic identifier with a capitalized first letter. This is a carryover from COM-based programming, which has interfaces such as IUnknown or IDirectDraw.

Unlike classes, which support single inheritance only, interfaces support multiple inheritance. The System.String class, for example, is derived from four interfaces provided within the System namespace:

public sealed class String:
    IComparable, ICloneable, IConvertible, IEnumerable { ... }

As C# programmers, we have three different experiences of interfaces: (1) as users of concrete instances of either the System or user-introduced interfaces, (2) as providers of a new class implementation of either the System or user-introduced interfaces, (3) as providers of a new interface class. We look at each of these three aspects in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.181.47