PlayMusic
End Sub
Public Sub DoALittleDance()
End Sub
End Class
Public Class EntryPoint
Shared Sub Main()
Dim Dude As New TalentedPerson()
Dim Musician As IMusician = Dude
CHAPTER 3 n CLASSES AND STRUCTURES 38
Musician.PlayMusic()
Dude.PlayMusic()
Dude.DoALittleDance()
End Sub
End Class
The previous example defines an interface named IMusician. A class, TalentedPerson,
indicates that it wants to support the IMusician interface through the Implements keyword.
The class declaration is basically saying, ???I would like to enter into a contract to support the
IMusician interface, and I guarantee to support all the methods defined by that interface.???
The requirement of that interface is merely to support the PlayMusic method, which the
TalentedPerson class does so faithfully. It is customary to name an interface type with a leading
uppercase I. When reading code, this stands as a marker to indicate that the type in
question is, in fact, an interface.
Now, clients can access the PlayMusic method in one of two ways. They can either call it
through the object instance directly, or they can obtain an interface reference onto the object
instance and call the method through it. Because the TalentedPerson class supports the
IMusician interface, references to objects of that class are implicitly convertible to references
of IMusician.
Pages:
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94