How to Override Isortable Interface

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace Overriding_interface   
  6. {  
  7.     class Program   
  8.     {  
  9.         /// <summary>  
  10.         /// /////////http://msdn.microsoft.com/en-us/library/orm-9780596521066-01-13.aspx ////for theory .........very usfull  
  11.         /// </summary>  
  12.         /// <param name="args"></param>  
  13.         static void Main(string[] args)   
  14.         {  
  15.             Tester t = new Tester();  
  16.             t.Run();  
  17.             Console.ReadLine();  
  18.         }  
  19.     }  
  20.     interface IStorable   
  21.     {  
  22.         void Read();  
  23.         void Write();  
  24.     }  
  25.     public class Note: IStorable   
  26.     {  
  27.         public Note(string s)   
  28.         {  
  29.             Console.WriteLine("Creating Note with: {0}", s);  
  30.         }  
  31.   
  32.         // Note's version of Read( ) is virtual  
  33.         public virtual void Read()   
  34.         {  
  35.             Console.WriteLine("Note Read Method for IStorable");  
  36.         }  
  37.   
  38.         // Note's version of Write( ) is NOT virtual!  
  39.         public void Write()   
  40.         {  
  41.             Console.WriteLine("Note Write Method for IStorable");  
  42.         }  
  43.     }  
  44.     public class Document: Note {  
  45.         public Document(string s): base(s) {  
  46.             Console.WriteLine("Creating Document with: {0}", s);  
  47.         }  
  48.   
  49.         // override the Read method  
  50.         public override void Read()   
  51.         {  
  52.             Console.WriteLine("Overriding the Read method for Document!");  
  53.         }  
  54.   
  55.         // implement my own Write method  
  56.         public new void Write()   
  57.         {  
  58.             Console.WriteLine("Implementing a new Write method for Document!");  
  59.         }  
  60.     }  
  61.     class Tester   
  62.     {  
  63.         public void Run()   
  64.         {  
  65.             Note theNote = new Document("Test Document");  
  66.             theNote.Read();  
  67.             theNote.Write();  
  68.             Console.WriteLine("\n");  
  69.             IStorable isStorable = theNote as IStorable;  
  70.             if (isStorable != null)  
  71.             {  
  72.                 isStorable.Read();  
  73.                 isStorable.Write();  
  74.             }  
  75.             Console.WriteLine("\n");  
  76.             // This time create a reference to the derived type  
  77.             Document theDoc = new Document("Second Test");  
  78.             theDoc.Read();  
  79.             theDoc.Write();  
  80.             Console.WriteLine("\n");  
  81.             IStorable isStorable2 = theDoc as IStorable;  
  82.             if (isStorable != null)   
  83.             {  
  84.                 isStorable2.Read();  
  85.                 isStorable2.Write();  
  86.             }  
  87.         }  
  88.     }  
  89. }