More on Interfaces

The following is adapted from a MSDN page:

If a class implements two interfaces that contain a member with the same signature, then implementing that member on the class will cause both interfaces to use that member as their implementation. In the following example, all the calls to Paint invoke the same method.

     class Test 
     {
      static void Main()
      {
       SampleClass SC = new SampleClass();
       IControl Ctrl = (IControl) SC;   // Ctrl has access only to members of IControl
       ISurface Srfc = (ISurface) SC;   // Srfc has access only to members of ISurface

       // The following lines all call the same method.
       SC.Paint();
       ctrl.Paint();
       srfc.Paint();
      }
     }

     interface IControl
     {
      void Paint();
     }

     interface ISurface
     {
      void Paint();
     } 

     class SampleClass : IControl, ISurface
     {
      // Both ISurface.Paint and IControl.Paint call this method. 
      public void Paint()
      {
       Console.WriteLine("Paint method in SampleClass");
      }
     }

When this is run, the output is:

     Paint method in SampleClass
     Paint method in SampleClass
     Paint method in SampleClass

If the two interface members do not perform the same function, however, this can lead to an incorrect implementation of one or both of the interfaces. It is possible to implement an interface member explicitly, creating a class member that is only called through the interface, and is specific to that interface. This is accomplished by prefixing the class member with the name of the interface and a period. For example:

     public class SampleClass : IControl, ISurface
     {
      void IControl.Paint()
      {
       System.Console.WriteLine("IControl.Paint");
      }

      void ISurface.Paint()
      {
       System.Console.WriteLine("ISurface.Paint");
      }
     }

The class member IControl.Paint is only available through the IControl interface, and ISurface.Paint is only available through ISurface. Both method implementations are separate, and neither is available directly in the class. For example:

     // Call the Paint methods from Main.
     SampleClass Obj = new SampleClass();
     //Obj.Paint();  // Compiler error.

     IControl C = (IControl)Obj;        // C has access only to members of IControl  
     C.Paint();  // Calls IControl.Paint on SampleClass.

     ISurface S = (ISurface)Obj;        // S has access only to members of ISurface
     S.Paint(); // Calls ISurface.Paint on SampleClass.

When this is run, the output is:

     IControl.Paint
     ISurface.Paint


Multiple Inheritance of Interfaces

While C# does not allow multiple inheritance of classes, it does allow this for interfaces. An interface may have several base interfaces. An interface inherits all members of its base interfaces, and a class which implements must implement all the members of all the interfaces involved.

The following example is adapted from the C# Language Specification.

     interface IControl
     {
      void Print();
     }
     
     interface IIdentify: IControl
     {
      void PrintID();
     }

     interface ITellAll: IControl
     {
      void DumpAll();
     }

     interface IDisclose: IIdentify, ITellAll 
     {
     }

The base interfaces of IDisclose are IControl, IIdentify, and ITellAll. In other words, the IDisclose interface above inherits members PrintID and DumpAll as well as Print. (Of course, each of these could have other members as well, and they would accumulate.)

If I have classes whose headings are:

     public class ABC : IControl
     
     public class DEF : IIdentify

     public class GHI : ITellAll

     public class JKL : IDisclose

then: