Visual Basic Interface

In visual basic, Interface is same like class but the only difference is class can contain both declarations and implementation of methods, properties, and events but Interface will contain only the declarations of methodsproperties and events that a class or structure can implement.

 

The Interface in visual basic is more like a contract and the class or structure that implements an interface must provide the implementation for all the members that are specified in the interface definition.

 

Generally, visual basic will not support multiple inheritance of classes but that can be achieved by using Interface. In addition, a structure in c# cannot be inherited from another structure or class but that can be inherited by using interfaces.

 

In visual basic, we can define the interface by using Interface keyword. Following is the example of defining the interface using Interface keyword.

 

Interface IUser

    Sub GetDetails()

End Interface

If you observe the above code snippet, we defined an interface (IUser) using Interface keyword with GetDetails method signature. Now, the IUser interface can be implemented by any class or structure by providing a definition for GetDetails method.

 

To implement an interface in a class or structure, the syntax will be like as shown below.

 

Class User

    Inherits IUser

    Private Sub GetDetails() Implements IUser.GetDetails

        ' Method Implementation

    End Sub

End Class

If you observe the above code snippet, we inherited an interface (IUser) in a class (User) and implemented a defined interface method in the class.

 

In visual basic, the interface cannot be instantiated directly but, it can be instantiated by a class or structure that implements an interface. Following is the example of creating an instance for the interface in visual basic programming language.

 

Dim u As IUser = New User()

In visual basic, a class can inherit only from one class but we can implement a multiple interfaces in a class or structure by using interfaces.

 

By default, the members of interface are public and we are not allowed to include any other access modifiers. In visual basic, an interface can contain a methodsproperties, events, indexers but it can’t contain constants, fields, operators, instance constructors, finalizers or types.

Visual Basic Interface Example

Following is the example of creating and implementing an instance using a class in visual basic programming language.

 

Module Module1

    Interface IUser

        Sub GetDetails(ByVal x As String)

    End Interface

    Class User

        Implements IUser

        Public Sub GetDetails(ByVal a As String) Implements IUser.GetDetails

            Console.WriteLine("Name: {0}", a)

        End Sub

    End Class

    Class User1

        Implements IUser

        Public Sub GetDetails(ByVal a As String) Implements IUser.GetDetails

            Console.WriteLine("Location: {0}", a)

        End Sub

    End Class

    Sub Main(ByVal args As String())

        Dim u As IUser = New User()

        u.GetDetails("Suresh Dasari")

        Dim u1 As IUser = New User1()

        u1.GetDetails("Hyderabad")

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we created an interface IUser, two classes “User & User1” and implemented an interface IUser by providing an implementation for GetDetails() method. Here, we created an instance for interface “IUser” using User & User1 classes.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic (VB) Interface Example Result

Visual Basic Multiple Inheritance with Interface

As discussed, visual basic will not support multiple inheritance of classes but that can be achieved by using interface.

 

Following is the example of implementing a multiple inheritance using interfaces in visual basic programming language.

 

Module Module1

    Interface IName

        Sub GetName(ByVal x As String)

    End Interface

    Interface ILocation

        Sub GetLocation(ByVal x As String)

    End Interface

    Interface IAge

        Sub GetAge(ByVal x As Integer)

    End Interface

    Class User

        Implements IName, ILocation, IAge

        Public Sub GetName(ByVal a As String) Implements IName.GetName

            Console.WriteLine("Name: {0}", a)

        End Sub

        Public Sub GetLocation(ByVal a As String) Implements ILocation.GetLocation

            Console.WriteLine("Location: {0}", a)

        End Sub

        Public Sub GetAge(ByVal a As Integer) Implements IAge.GetAge

            Console.WriteLine("Age: {0}", a)

        End Sub

    End Class

    Sub Main(ByVal args As String())

        Dim u As User = New User()

        u.GetName("Suresh Dasari")

        u.GetLocation("Hyderabad")

        u.GetAge(32)

        Console.WriteLine(vbLf & "Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we created multiple interfaces and implemented those interfaces using User class to achieve multiple inheritance.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Multiple Inheritance with Interface Example Result

 

This is how we can achieve multiple inheritance using interface in visual basic programming language. 

Visual Basic Interface Overview

Following are the important features of interface in visual basic programming language.

 

  • In visual basic, interface can contain only declarations of members such as methodsproperties, indexers and events.
  • By default, the members of interface are public and we are not allowed to include any other access modifiers.
  • In visual basic, an interface cannot be instantiated directly, but it can be instantiated by a class or structure that implements an interface.
  • The class or structure that implements an interface must provide an implementation for all the members that are specified in the interface definition.
  • The class or structure can implement multiple interfaces. 

Topics Covered