Overriding Interfaces
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; namespace OverridingInterfaces { interface IStorable { void Read(); void Write(); } // Simplify Document to implement only IStorable class Document : IStorable { // the document constructor public Document(string s) { Console.WriteLine( "Creating document with: {0}", s); } // Make read virtual public virtual void Read() { Console.WriteLine( "Document Read Method for IStorable"); } // NB: Not virtual! public void Write() { Console.WriteLine( "Document Write Method for IStorable"); } } // Derive from Document class Note : Document { public Note(string s): base(s) { Console.WriteLine( "Creating note with: {0}", s); } // override the Read method public override void Read() { Console.WriteLine( "Overriding the Read method for Note!"); } // implement my own Write method public new void Write() { Console.WriteLine( "Implementing the Write method for Note!"); } } public class OverridingInterfacesTester { public void Run() { // Create a Document object Document theNote = new Note("Test Note"); // direct call to the methods theNote.Read(); theNote.Write(); Console.WriteLine("\n"); // cast the Document to IStorable IStorable isNote = theNote as IStorable; if (isNote != null) { isNote.Read(); isNote.Write(); } Console.WriteLine("\n"); // create a note object Note note2 = new Note("Second Test"); // directly call the methods note2.Read(); note2.Write(); Console.WriteLine("\n"); // Cast the note to IStorable IStorable isNote2 = note2 as IStorable; if (isNote != null) { isNote2.Read(); isNote2.Write(); } } static void Main() { OverridingInterfacesTester t = new OverridingInterfacesTester(); t.Run(); } } }