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:
- Contract Definition: Interfaces define a blueprint of what a class should do, not how it should do it.
- Multiple Implementation: A class can implement multiple interfaces, allowing it to inherit different behaviors from various sources.
- No Implementation: Interfaces themselves do not contain executable code.
- Reference Types: Interfaces are reference types, similar to classes.
- Abstraction: They promote abstraction by hiding the concrete implementation details.
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:
- Loose Coupling: Reduces dependencies between classes, making code more flexible and maintainable.
- Polymorphism: Enables treating objects of different classes uniformly through a common interface.
- Testability: Facilitates unit testing by allowing the creation of mock implementations of interfaces.
- Extensibility: Makes it easier to add new functionality without altering existing code.
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:
IEnumerable(Of T)
andIEnumerator(Of T)
: For iterating over collections.IDisposable
: For releasing unmanaged resources.IComparable(Of T)
: For comparing objects.ICloneable
: For creating copies of objects.