Extending Objects Through Interfaces

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Suppose that in the process of designing your solution, you decide that you want to create several objects that are closely related, and in fact require at least some of the same properties and methods. Also, in the future, you might need to add more objects that are related to these, and you'd like to make the process as easy as possible for yourself down the road. You can implement an interface that defines the properties and methods that these objects have in common.

As noted earlier, an interface is the set of properties, methods, and events that define an object's characteristics and behavior. Every object has an interface, whether it's a built-in or custom object. When you implement an interface in a class module, you take advantage of another object's interface to provide some or all of the properties and methods for that class. In this way you can extend your objects, relate them according to their functionality, and maximize your code's reusability. You can implement interfaces to take advantage of polymorphism. Polymorphism refers to the ability to create objects that have specific individual functionality, but that share something in common with a more general object.

Note   If you've programmed in object-oriented programming languages, you may be familiar with the concepts of polymorphism and inheritance. Inheritance refers to the ability of a class to derive members (and their functionality) from other classes. By implementing an interface, you can achieve polymorphism. However, interfaces don't provide true inheritance, because a class can implement only those members that are defined within an interface that it implements. For example, if Class B implements the interface for Class A, and Class C implements the interface for Class B, Class C must implement all the members of Class B, but it cannot directly implement members of Class A. In true inheritance, objects can derive characteristics from an entire hierarchy of other objects. For example, if Class B inherits from Class A, and Class C inherits from Class B, and so on, Class E can selectively derive all or some characteristics from Classes A, B, C, and D.

As a conceptual example of polymorphism, consider Control objects. There are several different control classes. For example, CommandButton controls are created from the CommandButton class, and TextBox controls are created from the TextBox class. You can create a variable of type CommandButton and assign to it a reference to a CommandButton control. You can do the same with a variable of type TextBox. You can't, however, assign a reference to a TextBox control to a variable of type CommandButton — you'll get an error if you try it.

On the other hand, both types of controls also have the more general type Control. If you create a variable of type Control, you can assign a reference to either a command button or a text box to the variable. This flexibility is indispensable in cases where you need to enumerate through all the controls on a form and set their Visible properties to True, for example. When you know what type of control your code will work with, it's better to create a variable of the more specific type, but when you don't know ahead of time, you can create a variable of type Control.

The samples discussed in this section implement custom interfaces similar to the way that individual controls support the same Control class interface. There are a couple of ways to do this. The following sections discuss both ways in the context of examples. First, though, the next section covers some of the basics of implementing interfaces.