.NET
  Home arrow .NET arrow Page 3 - Delving Deeper into Interface-Based Progra...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Delving Deeper into Interface-Based Programming
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-05-31

    Table of Contents:
  • Delving Deeper into Interface-Based Programming
  • Interfaces and Partial Types
  • Implementing Multiple Interfaces
  • Interfaces and Class Hierarchies

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More .NET Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming .NET Components, Second...
     

    Buy this book now. 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.

    .NET ARTICLES

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT