Section 13.3. Implementing More Than One Interface

   

13.3 Implementing More Than One Interface

Classes can derive from only one class (and if you don't explicitly derive from a class, then you implicitly derive from Object). Classes can implement any number of interfaces. When you design your class, you can choose not to implement any interfaces, you can implement a single interface, or you can implement two or more interfaces. For example, in addition to IStorable, you might have a second interface, ICompressible, for files that can be compressed to save disk space. If your Document class can be stored and it also can be compressed, you might choose to have Document implement both the IStorable and ICompressible interfaces.

Both IStorable and ICompressible are interfaces created for this book and are not part of the standard .NET Framework.

Example 13-2 shows the complete listing of the new ICompressible interface and demonstrates how you modify the Document class to implement the two interfaces.

Example 13-2. IStorable and ICompressible, implemented by Document
 Option Strict On Imports System Namespace InterfaceDemo     Interface IStorable         Sub Read( )         Sub Write(ByVal obj As Object)         Property Status( ) As Integer     End Interface 'IStorable     ' here's the new interface     Interface ICompressible         Sub Compress( )         Sub Decompress( )     End Interface 'ICompressible ' Document implements both interfaces     Public Class Document         Implements ICompressible, IStorable         ' the document constructor         Public Sub New(ByVal s As String)             Console.WriteLine("Creating document with: {0}", s)         End Sub 'New         ' implement IStorable         Public Sub Read( ) Implements IStorable.Read             Console.WriteLine("Implementing the Read Method for IStorable")         End Sub 'Read         Public Sub Write(ByVal o As Object) Implements IStorable.Write             Console.WriteLine( _               "Implementing the Write Method for IStorable")         End Sub 'Write       Public Property Status( ) As Integer Implements IStorable.Status             Get                 Return myStatus             End Get             Set(ByVal Value As Integer)                 myStatus = Value             End Set         End Property         ' implement ICompressible         Public Sub Compress( ) Implements ICompressible.Compress             Console.WriteLine("Implementing Compress")         End Sub 'Compress         Public Sub Decompress( ) Implements ICompressible.Decompress             Console.WriteLine("Implementing Decompress")         End Sub 'Decompress         ' hold the data for IStorable's Status property         Private myStatus As Integer = 0     End Class 'Document     Class Tester         Public Sub Run( )             Dim doc As New Document("Test Document")             doc.Status = -1             doc.Read( )             doc.Compress( )             Console.WriteLine("Document Status: {0}", doc.Status)         End Sub 'Run         Shared Sub Main( )             Dim t As New Tester( )             t.Run( )         End Sub 'Main     End Class 'Tester End Namespace 'InterfaceDemo 
  Output:  Creating document with: Test Document Implementing the Read Method for IStorable  Implementing Compress  Document Status: -1 

As Example 13-2 shows, you declare the fact that your Document class will implement two interfaces by changing the declaration (in the list of interface bases) to indicate that both interfaces are implemented, separating the two interfaces with commas:

 Public Class Document  Implements ICompressible, IStorable  

After you've done this, the Document class must also implement the methods specified by the ICompressible interface. ICompressible has only two methods , Compress( ) and Uncompress( ), which are specified as:

 Interface ICompressible         Sub Compress( )         Sub Decompress( ) End Interface 'ICompressible 

In this simplified example, Document implements these two methods as follows , printing notification messages to the console:

 Public Sub Compress( ) Implements ICompressible.Compress      Console.WriteLine("Implementing Compress")  End Sub 'Compress    Public Sub Decompress( ) Implements ICompressible.Decompress      Console.WriteLine("Implementing Decompress") End Sub 'Decompress 
   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net