I’m sure you’re aware of Classes, which on a high-level are user-defined types, and their instances are objects. Vb.NET has introduced a new type, called an Interface. An Interface, according to this article by Nick Harrison, “is a contract that defines the signature of some piece of functionality”. This contract is fulfilled by the Implementing Class. The implementing class must execute “the contract”, or in other words, all the methods and properties of the Interface.
The following is an example of an interface IMyInterface which is about to bind a class with a contract to execute 2 methods (ShowMyData, GetMyData) and 2 properties (MyAddress, MyAge):
Interface IMyInterface Sub ShowMyData(ByVal obj1 As Object, ByVal obj2 As Object) Sub GetMyData() Property MyAddress() As [String] Property MyAge() As Integer End Interface ' IMyInterface
Note (above) the prototypes ShowMyData, GetMyData, MyAddress and MyAge: they do not have the identifier Public/Private in front of them, nor do they have their ending statements (End Sub, End Property). That is because by default, an Interface includes all public properties that will be accessible to the ‘implemented’ Interface as well as other calling Classes.
Classes that implement their interfaces must have the keywords: Implements InterfaceName. Also, methods of the class that implement the contract must also have the ‘implements’ keyword pointing to the method of the interface. Please note that, not all methods in the class must implement the methods of the Interface. For example:
Class MyClass Implements IMyInterface Public Sub ShowMyData(ByVal addressObj As Object, ByVal ageObj As Object) _ Implements IMyInterface.ShowMyData 'code End Sub Public Sub GetMyData() Implements IMyInterface.GetMyData 'code End Sub Public Property MyAddress() As String Implements IMyInterface.MyAddress 'code End Property Public Property MyAge() As Integer Implements IMyInterface.MyAge 'code End Property End Class
Example
Option Explicit On Option Strict On Namespace InterfaceExample Interface StudentInformation Sub Add(FirstName As String, LastName As String) Function NumberOfStudentsEnrolled() As Double Sub DisplayInfo() End Interface Public Class Student : Implements StudentInformation Private FirstName As String, LastName As String Private studentsEnrolled As Double = 0 Public Sub New() End Sub Public Sub Add(FirstName As String, LastName As String) Implements StudentInformation.Add Me.FirstName = FirstName Me.LastName = LastName studentsEnrolled += 1 End Sub Public Sub DisplayInfo() Implements StudentInformation.DisplayInfo Console.WriteLine("{0} has been enrolled", LastName & ", " & FirstName) End Sub Public Function NumberOfStudentsEnrolled() As Double Implements StudentInformation.NumberOfStudentsEnrolled Console.WriteLine("Total student enrolled: {0}", studentsEnrolled) Return studentsEnrolled End Function End Class Class Program Public Shared Sub Main(args As String()) Dim students As New Student() students.Add("John", "Smith") students.DisplayInfo() students.Add("Anna", "Joe") students.DisplayInfo() Dim studentsEnrolled As Double = students.NumberOfStudentsEnrolled() Console.ReadLine() End Sub End Class End Namespace
Output

Implementing Multiple Interfaces
Classes can derive from only one class. By default, when they do not explicitly derive from a class, they implicitly derive from an Object. However, classes can implement any number of Interfaces. The example below shows the above example implemting 2 interfaces.
Namespace InterfaceExample Interface IStudentInformation Sub Add(FirstName As String, LastName As String) Function NumberOfStudentsEnrolled() As Double Sub DisplayInfo() End Interface Interface ISubjects Sub AddNewSubject(SubjectName As String) Function TotalSubjects() As Integer Sub DisplayInfo() End Interface Public Class Student Implements IStudentInformation Implements ISubjects Private FirstName As String, LastName As String Private studentsEnrolled As Double = 0 Private totalSubjects As Integer = 0 Public Sub New() End Sub Public Sub Add(FirstName As String, LastName As String) Implements IStudentInformation.Add Me.FirstName = FirstName Me.LastName = LastName studentsEnrolled += 1 End Sub Private Sub IStudentInformation_DisplayInfo() Implements IStudentInformation.DisplayInfo Console.WriteLine("{0} has been enrolled", LastName & ", " & FirstName) End Sub Public Function NumberOfStudentsEnrolled() As Double Implements IStudentInformation.NumberOfStudentsEnrolled Console.WriteLine("Total student enrolled: {0}", studentsEnrolled) Return studentsEnrolled End Function Public Sub AddNewSubject(SubjectName As String) Implements ISubjects.AddNewSubject Console.WriteLine("Subject {0} was added", SubjectName) totalSubjects += 1 End Sub Public Function TotalSubjects() As Integer Implements ISubjects.TotalSubjects Return totalSubjects End Function Private Sub ISubjects_DisplayInfo() Implements ISubjects.DisplayInfo Console.WriteLine("Number of Subjects added: {0}", totalSubjects.ToString()) End Sub End Class Class Program Private Shared Sub Main(args As String()) Dim students As IStudentInformation = New Student() students.Add("John", "Smith") students.DisplayInfo() students.Add("Anna", "Joe") students.DisplayInfo() Dim studentsEnrolled As Double = students.NumberOfStudentsEnrolled() Dim subjects As ISubjects = New Student() subjects.AddNewSubject("Math") subjects.AddNewSubject("English") subjects.DisplayInfo() Console.ReadLine() End Sub End Class End Namespace
Output

{ 8 comments… read them below or add one }
Back in 2009 when this was posted you said you were going to cover multiple interfaces next, but now we are in 2011 and you have done so .
Kobie, I didn’t have anything planned on .NET but I will post a followup of this article very soon.
Cheers, Anshoo
I have included a section on multiple interfaces within this article. Also used a better example in comparison to what I did initially. Cheers, Anshoo
can you also provide the explanation of multiple interfaces implementation with your unique examples.
Deep, I will try to post something on this topic very soon.
Cheers
Anshoo,
Thanks for sharing and explaining the good article’s.
are there any new articles coming in this line.
Deep, nothing is planned on .NET in the coming few weeks. But, I will try to write something on your proposed topic soon..
How we can call / access classes/Interfaces developed in VB.net from QTP ? is there any direct method for it instead of dll and EXE Methods?