VB.NET Interfaces

Interfaces are a fundamental concept in object-oriented programming, especially in languages like VB.NET that support multiple inheritance of types. An interface defines a contract that classes must implement. It specifies a set of method signatures, properties, events, and indexers that a class promises to provide.

What is an Interface?

An interface is a reference type that can only contain declarations (signatures) of methods, properties, events, and indexers. It cannot contain implementation details like method bodies or field declarations. Classes, structures, and other interfaces can implement one or more interfaces.

Key Characteristics of Interfaces:

Declaring an Interface

You declare an interface using the Interface keyword. Members within an interface are implicitly Public and Abstract.


Public Interface IShape
    Property Area As Double
    Function GetPerimeter() As Double
    Sub Draw()
End Interface

Implementing an Interface

A class or structure implements an interface using the Implements keyword. The implementing type must provide an implementation for all members declared in the interface.


Public Class Circle
    Implements IShape

    Private _radius As Double

    Public Sub New(radius As Double)
        _radius = radius
    End Sub

    Public ReadOnly Property Area As Double Implements IShape.Area
        Get
            Return Math.PI * _radius * _radius
        End Get
    End Property

    Public Function GetPerimeter() As Double Implements IShape.GetPerimeter
        Return 2 * Math.PI * _radius
    End Function

    Public Sub Draw() Implements IShape.Draw
        Console.WriteLine("Drawing a circle.")
    End Sub
End Class

Public Class Square
    Implements IShape

    Private _side As Double

    Public Sub New(side As Double)
        _side = side
    End Sub

    Public ReadOnly Property Area As Double Implements IShape.Area
        Get
            Return _side * _side
        End Get
    End Property

    Public Function GetPerimeter() As Double Implements IShape.GetPerimeter
        Return 4 * _side
    End Function

    Public Sub Draw() Implements IShape.Draw
        Console.WriteLine("Drawing a square.")
    End Sub
End Class

Using Interfaces

You can use variables of interface type to refer to objects of any class that implements that interface. This allows for treating different objects in a uniform way.


Sub DisplayShapeInfo(shape As IShape)
    Console.WriteLine($"Area: {shape.Area}")
    Console.WriteLine($"Perimeter: {shape.GetPerimeter()}")
    shape.Draw()
End Sub

' Example usage:
Dim myCircle As New Circle(5.0)
Dim mySquare As New Square(4.0)

DisplayShapeInfo(myCircle)
DisplayShapeInfo(mySquare)

Benefits of Using Interfaces:

Note on Interface vs. Abstract Class

While both interfaces and abstract classes support abstraction, interfaces define a contract with no implementation, whereas abstract classes can contain both abstract members (without implementation) and concrete members (with implementation).

Tip: Naming Conventions

It is a common convention in VB.NET to prefix interface names with 'I', such as IShape or IDisposable.

Commonly Used Interfaces: