Multiple inheritance refers to the ability to derive from more than one class. Visual Basic .NET does not support multiple inheritance. Classes can derive from only one class. If they don't explicitly derive from a class, then they implicitly derive from the Object class.
Classes can, however, implement any number of interfaces. The ability to implement multiple interfaces accomplishes much the same thing as the ability to derive from more than one class. In fact, many object-oriented programmers would argue that implementing multiple interfaces is superior to multiple inheritance because it provides the equivalent capabilities with less confusion.
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 can also be compressed, you might choose to have Document implement both the IStorable and ICompressible interfaces.
|
Example 8-2 shows the complete listing of the new ICompressible interface and demonstrates how you modify the Document class to implement the two interfaces.
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 8-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
These methods do no more than display notification messages to the console; in effect the methods are stubbed out.
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
|
|
| Top |