Implementing More than One Interface

Classes can derive from only one class (and if it doesn’t explicitly derive from a class, then it implicitly derives from Object).

Tip

Some languages, such as C++, support implementation from multiple base classes (called Multiple Inheritance or MI). In 10 years of working with C++, I never used MI except to demonstrate that it could be done, and in 6 years of working with C#, I’ve never missed MI.

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 compressed, you might choose to have Document implement both the IStorable and ICompressible interfaces.

Tip

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

using System;

namespace InterfaceDemo
{
   interface IStorable
   {
      void Read(  );
      void Write( object obj );
      int Status { get; set; }

   }// here's the new interface
   interface ICompressible
   {
      void Compress(  );
      void Decompress(  );
   }


   public class Document : IStorable, ICompressible
   {
      private int status = 0;

      public Document( string s )
      {
         Console.WriteLine( "Creating document with: {0}", s );
      }

      #region IStorable

      public void Read(  )
      {
         Console.WriteLine(
         "Implementing the Read Method for IStorable" );
      }

      public void Write( object o )
      {
         Console.WriteLine(
         "Implementing the Write Method for IStorable" );
      }

      public int Status
      {
         get { return status; }
         set { status = value; }
      }

      #endregion     // IStorable

      #region ICompressible

      public void Compress(  )
      {
         Console.WriteLine( "Implementing Compress" );
      }

      public void Decompress(  )
      {
         Console.WriteLine( "Implementing Decompress" );
      }

      #endregion  // ICompressible
   }


   class Tester
   {
      public void Run(  )
      {
         Document doc = new Document( "Test Document" );
         doc.Status = -1;
         doc.Read(  );          // invoke method from IStorable
         doc.Compress(  );      // invoke method from ICompressible
         Console.WriteLine( "Document Status: {0}", doc.Status );
      }

      static void Main(  )
      {
         Tester t = new Tester(  );
         t.Run(  );
      }
   }
}

The output looks like this:

    Creating document with: Test Document
    Implementing the Read Method for IStorableImplementing Compress
    Document Status: -1

As Example 13-2 shows, you declare the fact that your Document class will implement two interfaces by adding the second interface to the declaration (in the base list), separating the two interfaces with commas:

    public class Document :IStorable, ICompressible

Once 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
     {
       void Compress(  );
       void Decompress(  );
     }

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

    public void Compress(  )
    {
     Console.WriteLine("Implementing the Compress Method");
    }

    public void Decompress(  )
    {
     Console.WriteLine("Implementing the Decompress Method");
    }
..................Content has been hidden....................

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