Chapter 5. Interfacing .NET

The concept of separating the interface from the implementation has been around for some time. You have seen how code written against an abstract base class allows you to write applications that can grow over time and remain resistant to some of the more negative aspects of change. This is not a new idea. C++ programmers have been using the same technique for years. In fact, the open-closed principle (see Chapter 4) is based on this idea.

Visual Basic .NET provides a more formal declaration of this notion of separation called an interface. At the highest level, an interface contains a group of related methods to perform a task or describe a service. It is similar to an abstract base class that does not contain implementation, and it serves a similar purpose as well. It acts as a contract, describing a behavior that an object provides. Any object that implements an interface has to provide an implementation of everything included within the interface definition.

Although they have been with the language for some time now, interfaces are one of the most misunderstood features of VB.NET. Undoubtedly, interfaces will continue to be neglected in lieu of implementation inheritance. This is unfortunate because interface-based programming is as important now as it was in the past. And in terms of inheritance, it is not an either-or situation. Interfaces have their place alongside implementation inheritance. In fact, each principle and technique discussed ...

Get Object-Oriented Programming with Visual Basic .NET 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.