Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

Interfaces

Interfaces are like classes in that they define a set of properties, methods, and events. They vary from classes in that they don't provide an implementation; rather, they are implemented by classes.

An interface is like a contract with a class. If a class implements an interface, it must supply an implementation for every aspect of the interface.

The purpose of creating interfaces and using them in your application is to "future proof" your code. If all your code is based on a set of classes and the methods and data they define, and something changes in a class definition, such as a parameter, the code that uses the class is broken. Interfaces, on the other hand, can't be changed after they are published; however, the implementation of the interface can change.

With interfaces, you can design features into small, related groups. Implementation of the interfaces can be enhanced without changing existing code that uses the interfaces, thus minimizing compatibility problems. You add new features by creating new interfaces and implementations.

To create an interface, you use the Interface statement. This process is similar to a class definition without the implementation. The following code sample shows an interface definition, ICompression, for a data compression interface:

Interface ICompression
  Function Compress(ByVal FileName As String) As Boolean
  Function DeCompress(ByVal FileName As String) As Boolean
End Interface

The two functions, Compress() and DeCompress(), have no implementation. To provide an implementation for the ICompression interface, you define a new class using Implements to implement the interface as shown in the following class declaration:

Class CompressedFile
   Implements ICompression

   Function Compress(ByVal FileName As String) As Boolean
    ' Compress the file and return success
   End Function

   Function DeCompress(ByVal FileName As String) As Boolean
    ' DeCompress the file and return success
   End Function
End Class
  • + Share This
  • 🔖 Save To Your Account