Compartilhar via


Interface Implementation Examples in Visual Basic 

Classes that implement an interface must implement all its properties, methods, and events.

The following example defines two interfaces. The second interface, Interface2, inherits Interface1 and defines an additional property and method.

Interface Interface1
    Sub sub1(ByVal i As Integer)
End Interface

' Demonstrates interface inheritance.
Interface Interface2
    Inherits Interface1
    Sub M1(ByVal y As Integer)
    ReadOnly Property Num() As Integer
End Interface

The next example implements Interface1, the interface defined in the previous example:

Public Class ImplementationClass1
    Implements Interface1
    Sub Sub1(ByVal i As Integer) Implements Interface1.sub1
        ' Insert code here to implement this method.
    End Sub
End Class

The final example implements Interface2, including a method inherited from Interface1:

Public Class ImplementationClass2
    Implements Interface2
    Dim INum As Integer = 0
    Sub sub1(ByVal i As Integer) Implements Interface2.sub1
        ' Insert code here that implements this method.
    End Sub
    Sub M1(ByVal x As Integer) Implements Interface2.M1
        ' Insert code here to implement this method.
    End Sub

    ReadOnly Property Num() As Integer Implements _
       Interface2.Num
        Get
            Num = INum
        End Get
    End Property
End Class

See Also

Tasks

How to: Create and Implement Interfaces
Walkthrough: Creating and Implementing Interfaces

Reference

Interface Statement (Visual Basic)
Implements Statement

Concepts

Interfaces Overview
Interface Definition
Implements Keyword and Implements Statement
When to Use Interfaces