/* 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(" "); // cast the Document to IStorable IStorable isNote = theNote as IStorable; if (isNote != null) { isNote.Read(); isNote.Write(); } Console.WriteLine(" "); // create a note object Note note2 = new Note("Second Test"); // directly call the methods note2.Read(); note2.Write(); Console.WriteLine(" "); // 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(); } } }
Class Interface
Interface casting
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; namespace InterfaceDemo { interface IStorable { void Read(); void Write(object obj); int Status { get; set; } } // here's the new interface interface ICompressible { void Compress(); void Decompress(); } // Document implements both interfaces class Document : IStorable { // the document constructor public Document(string s) { Console.WriteLine("Creating document with: {0}", s); } // implement IStorable public void Read() { Console.WriteLine( "Implementing the Read Method for IStorable"); } public void Write(object o) { Console.WriteLine( "Implementing the Write Method for IStorable"); } public int Status { get { return status; } set { status = value; } } // hold the data for IStorable's Status property private int status = 0; } public class TesterInterfaceDemoCasting { [STAThread] static void Main() { Document doc = new Document("Test Document"); // only cast if it is safe if (doc is IStorable) { IStorable isDoc = (IStorable) doc; isDoc.Read(); } else { Console.WriteLine("Could not cast to IStorable"); } // this test will fail if (doc is ICompressible) { ICompressible icDoc = (ICompressible) doc; icDoc.Compress(); } else { Console.WriteLine("Could not cast to ICompressible"); } } } }
Interface demo: implements two interfaces
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; namespace InterfaceDemo { interface IStorable { void Read(); void Write(object obj); int Status { get; set; } } // here's the new interface interface ICompressible { void Compress(); void Decompress(); } // Document implements both interfaces class Document : IStorable, ICompressible { // the document constructor public Document(string s) { Console.WriteLine("Creating document with: {0}", s); } // implement IStorable public void Read() { Console.WriteLine( "Implementing the Read Method for IStorable"); } public void Write(object o) { Console.WriteLine( "Implementing the Write Method for IStorable"); } public int Status { get { return status; } set { status = value; } } // implement ICompressible public void Compress() { Console.WriteLine("Implementing Compress"); } public void Decompress() { Console.WriteLine("Implementing Decompress"); } // hold the data for IStorable's Status property private int status = 0; } public class TesterInterfaceDemo2 { public void Run() { Document doc = new Document("Test Document"); doc.Status = -1; doc.Read(); doc.Compress(); Console.WriteLine("Document Status: {0}", doc.Status); } [STAThread] static void Main() { TesterInterfaceDemo2 t = new TesterInterfaceDemo2(); t.Run(); } } }
Interface demo
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; namespace InterfaceDemo { // define the interface interface IStorable { void Read(); void Write(object obj); int Status { get; set; } } // create a Document class that implements the IStorable interface class Document : IStorable { public Document(string s) { Console.WriteLine("Creating document with: {0}", s); } // implement the Read method public void Read() { Console.WriteLine( "Implementing the Read Method for IStorable"); } // implement the Write method public void Write(object o) { Console.WriteLine( "Implementing the Write Method for IStorable"); } // implement the property public int Status { get{ return status; } set{ status = value; } } // store the value for the property private int status = 0; } public class TesterInterfaceDemo { public void Run() { Document doc = new Document("Test Document"); doc.Status = -1; doc.Read(); Console.WriteLine("Document Status: {0}", doc.Status); } [STAThread] static void Main() { TesterInterfaceDemo t = new TesterInterfaceDemo(); t.Run(); } } }
Demonstrates the use of a simple interface
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // IntrFace.cs -- demonstrates the use of a simple interface // // Compile this program with the following command line: // C:>csc IntrFace.cs using System; namespace nsInterface { interface IPlane { double Area { get; } } interface ISolid { double Volume { get; } } class clsCircle : IPlane { public clsCircle (double radius) { m_Radius = radius; } public double Area { get {return (3.14159 * m_Radius * m_Radius);} } private double m_Radius; public override string ToString () { return ("Area = " + Area); } } class clsSphere : IPlane, ISolid { public clsSphere (double radius) { m_Radius = radius; } public double Area { get {return (4 * 3.14159 * m_Radius * m_Radius);} } public double Volume { get {return (4 * 3.14159 * m_Radius * m_Radius * m_Radius / 3);} } private double m_Radius; public override string ToString () { return ("Area = " + Area + ", " + "Volume = " + Volume); } } public class IntrFace { static public void Main () { clsCircle circle = new clsCircle (14.2); clsSphere sphere = new clsSphere (16.8); Console.WriteLine ("For the circle: " + circle); Console.WriteLine ("For the sphere: " + sphere); } } }
illustrates interface member hiding
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example8_8.cs illustrates interface member hiding */ using System; // define the IDrivable interface public interface IDrivable { void TurnLeft(); } // define the ISteerable interface (derived from IDrivable) public interface ISteerable : IDrivable { new void TurnLeft(); // hides TurnLeft() in IDrivable } // Car class implements the IMovable interface class Car : ISteerable { // explicitly implement the TurnLeft() method of the ISteerable interface void ISteerable.TurnLeft() { Console.WriteLine("ISteerable implementation of TurnLeft()"); } // implement the TurnLeft() method of the IDrivable interface public void TurnLeft() { Console.WriteLine("IDrivable implementation of TurnLeft()"); } } public class Example8_8 { public static void Main() { // create a Car object Car myCar = new Car(); // call myCar.TurnLeft() Console.WriteLine("Calling myCar.TurnLeft()"); myCar.TurnLeft(); // cast myCar to ISteerable ISteerable mySteerable = myCar as ISteerable; Console.WriteLine("Calling mySteerable.TurnLeft()"); mySteerable.TurnLeft(); // cast myCar to IDrivable IDrivable myDrivable = myCar as IDrivable; Console.WriteLine("Calling myDrivable.TurnLeft()"); myDrivable.TurnLeft(); } }
illustrates an explicit interface member implementation
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example8_7.cs illustrates an explicit interface member implementation */ using System; // define the IDrivable interface public interface IDrivable { void TurnLeft(); } // define the ISteerable interface public interface ISteerable { void TurnLeft(); } // Car class implements the IMovable interface class Car : IDrivable, ISteerable { // explicitly implement the TurnLeft() method of the IDrivable interface void IDrivable.TurnLeft() { Console.WriteLine("IDrivable implementation of TurnLeft()"); } // implement the TurnLeft() method of the ISteerable interface public void TurnLeft() { Console.WriteLine("ISteerable implementation of TurnLeft()"); } } public class Example8_7 { public static void Main() { // create a Car object Car myCar = new Car(); // call myCar.TurnLeft() Console.WriteLine("Calling myCar.TurnLeft()"); myCar.TurnLeft(); // cast myCar to IDrivable IDrivable myDrivable = myCar as IDrivable; Console.WriteLine("Calling myDrivable.TurnLeft()"); myDrivable.TurnLeft(); // cast myCar to ISteerable ISteerable mySteerable = myCar as ISteerable; Console.WriteLine("Calling mySteerable.TurnLeft()"); mySteerable.TurnLeft(); } }