Defining Interfaces

An interface is a reference type which is defined within an Interface..End Interface block. Interfaces define only signatures for members that classes will then expose and are basically a set of the members’ definitions. Imagine you want to create an interface that defines members for working with documents. This is accomplished with the following code:

image

The interface is marked as Public because the default scope for interfaces is Friend. Assigning public visibility ensures external assemblies use the interface (which is a common scenario).

Interfaces Scope

Interfaces can be declared Private, Protected, or Protected Friend only if they are defined within a type such as a class.

As a convention, interface identifiers begin with a capital I. This is not mandatory (except when creating CLS-compliant interfaces), but I strongly recommend you follow the convention. The most important consideration is that the interface definition contains only members’ definitions with no body. For both Load and Save methods’ definition, there is only a signature but not the method body and implementation, which are left to classes that implement the interface. Another important thing is that members defined within interfaces cannot be marked with one of the scope qualifiers, such as Public, Friend, and so on. By default, members defined by interfaces are Public. Finally, being reference types, interfaces need to be treated as such. See Chapter 4, “Data Types and Expressions”, for further information on reference types.

Nested Classes

Interfaces can surprisingly define classes. A class defined within an interface is a typical Class..End Class block as you would normally define one. This is certainly an uncommon scenario and can be useful to avoid naming conflicts with other classes, but you have to know that it is possible.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.104.5