2.12 Multiple Inheritance Using Interfaces

 <  Day Day Up  >  

You want your class to inherit more than one interface.


Technique

For each interface you want your class to support, add the interface name to the derivation list of your class, separating it from the other interfaces with a comma.

Comments

The C# specification states that classes must use single inheritance when deriving from base classes. You can only specify a single base class when creating a new class. However, you are free to multiply inherit from interfaces, meaning you can inherit from several. If you recall from the earlier discussion, an interface by itself does not contain an implementation. It is simply a declaration of methods and properties that a class must define.

With multiple interface inheritance, it is possible to implement two interfaces that contain a method with the same signature. Although it might be rare, it can happen. If this occurs, you have one of two choices. You can simply create a single method that will then be called regardless of the type of the reference to your class a client is using. Listing 2.12 demonstrates how to do so. It declares 2 interfaces, IVolumeControl and IChannelControl , and each of them contain the same property, Current . The Radio class implements these two interfaces, and any time the Current property is accessed, it calls the only Current property defined within the Radio class, regardless of which interface the client is referencing.

Listing 2.12 Declaring 2 Interfaces, IVolumeControl and IChannelControl
 using System; namespace _12_MultipleInterfaces {     interface IVolumeControl     {         int Current         {             get;         }     }     interface IChannelControl     {         int Current         {             get;         }     }     public class Radio : IVolumeControl, IChannelControl     {         public int Current         {             get             {                 return 5;             }         }     }     class Class1     {         [STAThread]         static void Main(string[] args)         {            // create new radio object and get interface            IChannelControl radioDial = (IChannelControl) new Radio();            // output current radio's channel            Console.WriteLine( "Current Channel = {0}", radioDial.Current );         }     } } 

You can also create two methods to separate the implementations . To do so, remove the accessibility modifier from each method signature and qualify the name of the method with the name of the interface to which it belongs. You can see this step in Listing 2.13. A client using this class will then have to cast the object instance to one of the interface types to correctly call the property methods.

Listing 2.13 Implementing Multiple Interfaces
 using System; namespace _12_MultipleInterfaces {     // interface declarations omitted     public class Radio : IVolumeControl, IChannelControl     {         int IVolumeControl.Current         {             get             {                 return 1;             }         }         int IChannelControl.Current         {             get             {                 return 2;             }         }     }     class Class1     {         [STAThread]         static void Main(string[] args)         {            IChannelControl radioDial = (IChannelControl) new Radio();            Console.WriteLine( "Current Channel = {0}", radioDial.Current );         }     } } 
 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net