using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Overriding_interface
{
class Program
{
/// <summary>
/// /////////http://msdn.microsoft.com/en-us/library/orm-9780596521066-01-13.aspx ////for theory .........very usfull
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Tester t = new Tester();
t.Run();
Console.ReadLine();
}
}
interface IStorable
{
void Read();
void Write();
}
public class Note: IStorable
{
public Note(string s)
{
Console.WriteLine("Creating Note with: {0}", s);
}
// Note's version of Read( ) is virtual
public virtual void Read()
{
Console.WriteLine("Note Read Method for IStorable");
}
// Note's version of Write( ) is NOT virtual!
public void Write()
{
Console.WriteLine("Note Write Method for IStorable");
}
}
public class Document: Note {
public Document(string s): base(s) {
Console.WriteLine("Creating Document with: {0}", s);
}
// override the Read method
public override void Read()
{
Console.WriteLine("Overriding the Read method for Document!");
}
// implement my own Write method
public new void Write()
{
Console.WriteLine("Implementing a new Write method for Document!");
}
}
class Tester
{
public void Run()
{
Note theNote = new Document("Test Document");
theNote.Read();
theNote.Write();
Console.WriteLine("\n");
IStorable isStorable = theNote as IStorable;
if (isStorable != null)
{
isStorable.Read();
isStorable.Write();
}
Console.WriteLine("\n");
// This time create a reference to the derived type
Document theDoc = new Document("Second Test");
theDoc.Read();
theDoc.Write();
Console.WriteLine("\n");
IStorable isStorable2 = theDoc as IStorable;
if (isStorable != null)
{
isStorable2.Read();
isStorable2.Write();
}
}
}
}In this code you will see the How to Override Isortable Interface.
