Overriding Interfaces : Interface « Class Interface « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Class Interface » InterfaceScreenshots 
Overriding Interfaces
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();
         }
     }
 }

           
       
Related examples in the same category
1.Interface with method name conflicts
2.Demonstrate the ByTwos interfaceDemonstrate the ByTwos interface
3.Demonstrate the ByTwos interface 2Demonstrate the ByTwos interface 2
4.Use a property in an interfaceUse a property in an interface
5.Add an indexer in an interfaceAdd an indexer in an interface
6.One interface can inherit anotherOne interface can inherit another
7.Explicitly implement an interface memberExplicitly implement an interface member
8.Use explicit implementation to remove ambiguityUse explicit implementation to remove ambiguity
9.Two class inherit one interfaceTwo class inherit one interface
10.Using interface 3Using interface 3
11.illustrates interfacesillustrates interfaces
12.illustrates implementing multiple interfacesillustrates implementing multiple interfaces
13.illustrates inheriting from a class and implementing multiple interfacesillustrates inheriting from a class and implementing multiple interfaces
14.illustrates casting an object to an interfaceillustrates casting an object to an interface
15.illustrates deriving an interface from one interfaceillustrates deriving an interface from one interface
16.illustrates deriving an interface from multiple interfacesillustrates deriving an interface from multiple interfaces
17.illustrates an explicit interface member implementationillustrates an explicit interface member implementation
18.illustrates interface member hidingillustrates interface member hiding
19.Demonstrates the use of a simple interfaceDemonstrates the use of a simple interface
20.Interface demoInterface demo
21.Interface demo: implements two interfacesInterface demo: implements two interfaces
22.Interface castingInterface casting
23.Overriding Interfaces: Tester Overriding InterfacesAsOverriding Interfaces: Tester Overriding InterfacesAs
24.A safe method of determining whether a class implements a particular interfaceA safe method of determining whether a class implements a particular interface
25.Interfaces:Working with InterfacesInterfaces:Working with Interfaces
26.Reimplementation of Interfaces
27.Interface with event
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.