views:

126

answers:

3
  • I have a File class that has an Open() method.
  • I have a subclass of File called TextFile that implements an IReadableFile interface, which requires the implementation of a Read() method.

If I declare a variable myFile as IReadableFile, I can't call the Open() method on it. I want to be able to exercise the functionality of TextFile's base class (File) methods and its interface (IReadableFile) methods at once. Is this possible?

EDIT: I'm working in VB.NET (if that matters).

I'm trying to provide a minimum set of File I/O functionality via a File class and then provide extended capabilities for particular types of files by deriving from File and adding some additional methods (like Read, Write, etc.). I want the derived classes to be polymorphic - e.g., calling the Write method on a TextFile will simply write the text data to the filesystem, whereas calling the Write method on a BinaryFile might base 64 encode the binary data before writing it to the filesystem.

+4  A: 

Create an IFile interface, make the File class implements IFile. The IReadableFile will inherits from IFile.

Fabian Vilers
A: 

Its only possible if you put Open() in IReadableFile or cast your IReadableFile to a TextFile.

More details about what you're doing might help.

Will
A: 

You can declare myFile as a TextFile object. Then you will be able to call Read (from the inherited File object) and Open (from the implemented IFileReadable interface).

Public Class File

  Public Sub Open()

  End Sub

End Class

Public Interface IFileReadable
  Sub Read()
End Interface

Public Class TextFile
  Inherits File
  Implements IFileReadable

  Public Sub Read() Implements IFileReadable.Read

  End Sub

End Class

Public Class Main

  Public Sub New()

    Dim oFile As TextFile
    oFile.Read()
    oFile.Open()

  End Sub

End Class
Brandon Montgomery