Share via


Implements Keyword and Implements Statement 

The Visual Basic reserved word Implements is used in two ways. The Implements statement signifies that a class or structure implements an interface. The Implements keyword signifies that a class member or structure member implements a specific interface member.

Implementing Statement

If a class or structure implements one or more interfaces, it must include the Implements statement immediately after the Class or Structure statement. The Implements statement requires a comma-separated list of interfaces to be implemented by a class. The class or structure must implement all interface members using the Implements keyword.

Implements Keyword

The Implements keyword requires a comma-separated list of interface members to be implemented. Generally, only a single interface member is specified, but you can specify multiple members. The specification of an interface member consists of the interface name, which must be specified in an implements statement within the class; a period; and the name of the member function, property, or event to be implemented. The name of a member that implements an interface member can use any legal identifier, and it is not limited to the InterfaceName_MethodName convention used in earlier versions of Visual Basic.

For example, the following code shows how to declare a subroutine named Sub1 that implements a method of an interface:

Class Class1
    Implements interfaceclass.interface2

    Sub Sub1(ByVal i As Integer) Implements interfaceclass.interface2.Sub1
    End Sub
End Class

The parameter types and return types of the implementing member must match the interface property or member declaration in the interface. The most common way to implement an element of an interface is with a member that has the same name as the interface, as shown in the previous example.

To declare the implementation of an interface method, you can use any attributes that are legal on instance method declarations, including Overloads, Overrides, Overridable, Public, Private, Protected, Friend, Protected Friend, MustOverride, Default, and Static. The Shared attribute is not legal since it defines a class rather than an instance method.

Using Implements, you can also write a single method that implements multiple methods defined in an interface, as in the following example:

Class Class2
    Implements I1, I2

    Protected Sub M1() Implements I1.M1, I1.M2, I2.M3, I2.M4
    End Sub
End Class

You can use a private member to implement an interface member. When a private member implements a member of an interface, that member becomes available by way of the interface even though it is not available directly on object variables for the class.

See Also

Tasks

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

Reference

Implements Statement
Implements (Visual Basic)

Concepts

Interfaces Overview
Interface Definition
Interface Implementation Examples in Visual Basic
When to Use Interfaces

Other Resources

Inheritance in Visual Basic