Overriding Interfaces: Tester Overriding InterfacesAs

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 namespace OverridingInterfaces
 {
     interface IStorable
     {
         void Read();
         void Write();
     }

     interface ITalk
     {
         void Talk();
         void Read();
     }


     // Modify Document to also implement ITalk
     class Document : IStorable, ITalk
     {
         // the document constructor
         public Document(string s)
         {
             Console.WriteLine(
                 "Creating document with: {0}", s);
         }

         // Implicit implementation
         public virtual void Read()
         {
             Console.WriteLine(
                 "Document Read Method for IStorable");
         }

         public void Write()
         {
             Console.WriteLine(
                 "Document Write Method for IStorable");
         }

         // Explicit implementation
         void ITalk.Read()
         {
             Console.WriteLine("Implementing ITalk.Read");
         }

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

    public class TesterOverridingInterfacesAs
    {
       [STAThread]
       static void Main()
       {
           // Create a Document object
           Document theDoc = new Document("Test Document");
           IStorable isDoc = theDoc as IStorable;
           if (isDoc != null)
           {
               isDoc.Read();
           }

           // Cast to an ITalk interface
           ITalk itDoc = theDoc as ITalk;
           if (itDoc != null)
           {
               itDoc.Read();
           }

           theDoc.Read();
           theDoc.Talk();
       }
    }
 }