Classes can derive from only one class (and if it doesn’t explicitly derive from a class, it implicitly derives from Object
).
Some languages, such as C++, support inheritance from multiple base classes. C# allows inheritance from only a single class, but interfaces don’t have that limitation.
When you design your class, you can choose not to implement any interfaces, you can implement a single interface, or you can implement more than one. For example, in addition to IStorable
, you might have a second interface, ICompressible
, for files that can be compressed to save disk space. This new interface might have methods of Compress( )
and Decompress( )
, for example. If your Document
class can be stored and 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. Implementing multiple interfaces isn’t much more difficult than implementing a single one; you just have to implement the required methods for both interfaces
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Example_13_2_ _ _ _Multiple_Interfaces { 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 { public Document(string s) { Console.WriteLine("Creating document with: {0}", s); } #region IStorable public void Read( ) { Console.WriteLine("Executing Document's Read Method for IStorable"); } public void Write(object o) { Console.WriteLine("Executing Document's Write Method for IStorable"); } public int Status{ get; set;} #endregion // IStorable #region ICompressible public void Compress( ) { Console.WriteLine("Executing Document's Compress Method for ICompressible"); } public void Decompress( ) { Console.WriteLine("Executing Document's Decompress Method for ICompressible"); } #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( ); } } }
Creating document with: Test Document
Executing Document's Read Method for IStorable
Executing Document's Compress Method for ICompressible
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 Decompress( )
, 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("Executing Document's Compress Method for ICompressible"); } public void Decompress( ) { Console.WriteLine("Executing Document's Decompress Method for ICompressible"); }
Once again, these methods don’t really do anything other than output a message announcing their intentions; that’s deliberate, to keep the example short.
As you can see, implementing multiple interfaces isn’t hard at all; each interface mandates additional methods that your class has to provide. You could implement several interfaces in this way.