Implementing More Than One Interface

Multiple inheritance refers to the ability to derive from more than one class. Visual Basic .NET does not support multiple inheritance. Classes can derive from only one class. If they don’t explicitly derive from a class, then they implicitly derive from the Object class.

Classes can, however, implement any number of interfaces. The ability to implement multiple interfaces accomplishes much the same thing as the ability to derive from more than one class. In fact, many object-oriented programmers would argue that implementing multiple interfaces is superior to multiple inheritance because it provides the equivalent capabilities with less confusion.

When you design your class you can choose not to implement any interfaces, you can implement a single interface, or you can implement two or more interfaces. For example, in addition to IStorable, you might have a second interface, ICompressible, for files that can be compressed to save disk space. If your Document class can be stored and it can also be compressed, you might choose to have Document implement both the IStorable and ICompressible interfaces.

Tip

Both IStorable and ICompressible are interfaces created for this book and are not part of the standard .NET Framework.

Example 8-2 shows the complete listing of the new ICompressible interface and demonstrates how you modify the Document class to implement the two interfaces.

Example 8-2. IStorable and ICompressible, implemented by Document ...

Get Programming Visual Basic .NET, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.