14.2 Implementing More Than One InterfaceClasses 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 compressed, you might choose to have Document implement both the IStorable and ICompressible interfaces.
Example 14-2 shows the complete listing of the new ICompressible interface and demonstrates how you modify the Document class to implement the two interfaces. Example 14-2. IStorable and ICompressible, implemented by Documentusing 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(); } // Document implements both interfaces public class Document : IStorable, ICompressible { // the document constructor public Document(string s) { Console.WriteLine("Creating document with: {0}", s); } // implement 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; } } // implement ICompressible public void Compress() { Console.WriteLine("Implementing Compress"); } public void Decompress() { Console.WriteLine("Implementing Decompress"); } // hold the data for IStorable's Status property private int status = 0; } class Tester { public void Run() { Document doc = new Document("Test Document"); doc.Status = -1; doc.Read(); doc.Compress(); Console.WriteLine("Document Status: {0}", doc.Status); } [STAThread] static void Main() { Tester t = new Tester(); t.Run(); } } } Output: Creating document with: Test Document Implementing the Read Method for IStorable Implementing Compress Document Status: -1 As Example 14-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"); } |