Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Extending Objects Through Interfaces

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 application, 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 have to add more objects that are related to these, and you want 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 is 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 have programmed in object-oriented programming languages, you might 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 do not 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 cannot, 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 must 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 is better to create a variable of the more specific type, but when you do not know ahead of time, you can create a variable of type Control.

See Also

Why Build Your Own Objects? | Interface Basics | Implementing an Abstract Interface | Implementing a Nonabstract Class | Basic Class Concepts | Creating Property Procedures | Creating Events and Event Procedures | Designing Object Models | Creating Custom Objects for Web Pages

Show:
© 2015 Microsoft