How to: Define Interfaces in VB.NET

The interface is a set of definitions of properties, methods and events. Unlike classes […], interfaces do not contain the implementation. Interfaces are implemented […] by classes, but are defined as separate entities.
A class that implement an interface must implement all elements defined in that interface.

Defining a VB.NET interface is achieved using the specification Interface and interface implementation using specification Implements.
The definition of an interface can retrieve specifications defined in other interfaces. Inheritance of elements defined in other components are implemented in VB.NET language mentioning specification Inherits.
In a namespace […], interfaces have associated the modifier Friend implicitly, and interfaces defined in classes, modules, interfaces and structures have associated modifier Public.
Interface is a powerful tool of programming, because the objects definition and the implementation are separated. Cases in which it is recommended to define interfaces:

  • Classes with high orthogonality: small-scale implementation of inheritance to define classes;
  • High flexibility: a class can implement multiple interfaces;
  • Implementation inheritance is not desired from a base class;
  • Inheritance cannot be used: structures […] cannot inherit classes, but can implement interfaces.

Example of defining an interface in VB.NET:

Interface IOperatii
    Event Calcul(ByVal x As Integer, ByVal y As Integer)
    Function OpDiferenta(ByVal a As Integer, ByVal b As Integer) As Integer
    Function OpProdus(ByVal a As Integer, ByVal b As Integer) As Long
End Interface

In above example, the definition of interface IOperatii was done using the specification Interface and ended with specification End Interface.
Interface IOperatii defines an event called Calcul and two methods of type Function called OpDiferenta and OpProdus.
The application created with Visual Studio has the type Console Application.