Interfaces

Interfaces are one of the most important foundations of the Component Object Model. You can’t read more than a page or so of COM documentation without running into IReadThisBook, IDontUnderstand, or some other interface. By convention, interface names always start with the capital letter I followed by initial-cap words that describe the interface.

Actually, interfaces exist whether you use this convention or not. In fact, every Visual Basic class or form you create has a hidden interface that directly violates the standard. For example, if you create a class called CCallMeIshmael, Visual Basic will create an interface for it called _CCallMeIshmael.

I won’t go into all the details of how interfaces work, but I can recommend a good tool for exploring them on your own. The OleView program provided in the \TOOLS\OLETOOLS directory of the Visual Basic CD-ROM will tell you all you ever wanted to know about classes (coclasses to COM) and interfaces. It’s interesting to see the COM view of classes that you’re familiar with. For example, load OleView, click the View TypeLib button, and open VBCORE.DLL. Expand the CoClasses list and then expand the CDrive coclass.

The resulting information is a little overwhelming, but you can see that the C­Drive coclass has a _CDrive interface. The _CDrive interface shows the names, parameter types, and return type of methods and properties, but it doesn’t give any hints about the implementation. You can change the implementation of a class and clients won’t care in the least as long as you don’t change its interface. In fact, you can have several different classes with completely different implementations, but as long as they have the same interface, you can use them interchangeably. That’s polymorphism.