Delving Deeper into Interface-Based Programming - Implementing Multiple Interfaces
(Page 3 of 4 )
A class can derive from as many interfaces as required (see Example 3-5), but from at most one base class. When a class derives from a base class and from one or more interfaces, the base class must be listed first in the derivation chain (a requirement the compiler enforces):
public interface IMyInterface
{}
public interface IMyOtherInterface
{}
public class MyBaseClass
{}
public class MySubClass : MyBaseClass,IMyInterface,IMyOtherInterface
{}
Even such a trivial example raises a number of questions. What if both interfaces define identical methods? What are the available ways to resolve such collisions? What if the base class already derives from one or more of the interfaces?
When a class derives from two or more interfaces that define an identical method, you have two options: the first is to channel both interface methods to the same actual method implementation, and the second is to provide separate method implementations. For example, consider two interfaces that define the identical methodMethod1():
public interface IMyInterface
{
void Method1();
}
public interface IMyOtherInterface
{
void Method1();
}
If you want to channel both interface methods to the same method implementation, all you have to do is derive from the interfaces and implement the method once:
public class MyClass : IMyInterface,IMyOtherInterface
{
public void Method1()
{...}
//Other methods and members
}
Regardless of which interface the client ofMyClasschooses to use, calls toMethod1()will be channeled to that single implementation:
IMyInterface obj1;
IMyOtherInterface obj2;
obj1 = new MyClass();
obj1.Method1();
obj2 = obj1 as IMyOtherInterface;
Debug.Assert(obj2 != null);
obj2.Method1();
To provide separate implementations, use explicit interface implementation by qualifying the method implementation with the name of the interface that defines it:
public class MyClass : IMyInterface,IMyOtherInterface
{
void IMyInterface.Method1()
{...}
void IMyOtherInterface.Method1()
{...}
//Other methods and members
}
Now, when the client calls an interface method, that interface-specific method is called. You can even have separate explicit implementations for some of the
common methods and channel the others to the same implementation. However, as mentioned before, for the sake of consistency it’s better to avoid mixing and matching.
If you want to both use explicit interface implementation and channel the implementation from one interface to the other, you will need to use thethisreference to query for the desired interface and delegate the call:
public class MyClass : IMyInterface, IMOtherInterface
{
void IMyInterface.Method1 ( )
{...}
void IMyOtherInterface.Method1 ( )
{
IMyInterface myInterface = this;
myInterface.Method ( );
}
//Other methods and members
}
Using thethis reference this way is the only way to call an explicit interface method by its own implementing class.
Next: Interfaces and Class Hierarchies >>
More .NET Articles
More By O'Reilly Media
|
This article is excerpted from chapter three of Programming .NET Components, Second Edition, written by Juval Lowy (O'Reilly, 2006; ISBN: 0596007620). Check it out today at your favorite bookstore. Buy this book now.
|
|