How to Re-Implement interfaces in VB.NET
In this article you will learn how to re-implement interfaces in VB.NET
Suppose we have an interface implemented on a class and we want to create a subclass derived from the super class and change the implementation. The difference between the way of re-implementation is that in VB.NET you can not implement an interface on a subclass of a superclass that has already implemented it whereas In C# you can. Instead of implementing the interface a second time, you can override the superclass method that implements the interface method. Here I will explain how to re-implement interfaces in VB.NET, code is given below so you can take a first look:
Module Module1
Class BaseClass
Implements IFormattable
Implements IComparable
Public Value As String
Public Overridable Overloads Function ToString(ByVal _
Format As String, ByVal Provider As IFormatProvider) _
As String Implements IFormattable.ToString
ToString = Value
End Function
Public Overridable Overloads Function CompareTo(ByVal A _
As Object) As Integer Implements IComparable.CompareTo
If (Value = A.Value) Then
CompareTo = 0
ElseIf (Value < A.Value) Then
CompareTo = -1
Else
CompareTo = 1
End If
End Function
Public Sub New(ByVal Value As String)
Me.Value = Value
End Sub
End Class
Class DerivedClass
Inherits BaseClass
Public Overrides Function ToString(ByVal _
Format As String, ByVal Provider As IFormatProvider) _
As String
ToString = UCase(Value)
End Function
Public Sub New(ByVal Value As String)
MyBase.New(Value)
End Sub
End Class
Sub Main()
Dim A As New BaseClass ("Hello! how are you all?")
Dim B As New DerivedClass ("Welcome to VB.NET heaven")
Console.WriteLine(A)
Console.WriteLine(B)
Console.WriteLine(A.CompareTo(B))
Console.ReadLine()
End Sub
End Module
OUTPUT:
I hope now it is clear to you that how to Re-Implement interface in VB.NET.