A newbie question on Interfaces

hi,

   I'm new to C# and OO in general. I can't seem to undestand the pertinence of interfaces.

  From my understanding, the interfaces define the signature of the methods while the class implements these methods. How is this better than simply doing away with interfaces and simply implementing the method in the class

  Apologies for the rather novice question, an example to illustrate the pertinence of interfaces would be greatly appreciated.

regards,
Pat



Answer this question

A newbie question on Interfaces

  • ryan031581

    Hi Daniel,

       In multiple inheritence though, the base classes can implement their own methods (not just create the signatures like interfaces do) that the derived classes can inherit and use. It seems to me that interfaces are somewhat similar to Virtual methods in C++ but the thing that i don't get is that if interfaces don't implement methods, how does that allow code reuse (stated as one of the advantages of interfaces)

    Thanks,
    Pat

  • pogar

    You're welcome!

  • windisfree

    Interfaces have the bonus of multiple inheritance. In C#, a class can only inherit from one base class, but can implement any number of interfaces.

  • louimercieca

    Lots of parentheses!  But I get it.  Thanks.
  • hamidjr

    Hi patricia,
    I think it can help u if u think of interface programming as a means to write polimorphic code. You must have noticed that while u use the .net framework you find yourself passing and recieving objects to and from the runtime or base class library. Lots of these methods expect that your objectx should implement interfacex (names r just examples here) before something is passed in. This is a pretty cool feature. I would encourage u to just go through the IComparable interface and see that how it is used to sort array of objects through Array.Sort method. It'll give u a good idea of what I said in this post.

    Regards,

    Abubakar.

  • Benjamin Arroyo

    I think what you really want to use is the ArrayList rather than Array.

  • john_j

    Arrays are kind of funky for some reason. I know the docs say that it implements the IList interface, but in playing around with it, the only way I can see to utilize this is to cast the array to the interface. So, you need something like this:

    int[] x = new int[3];

     ...
     ...

    if (((IList)x).Contains(5))
     MessageBox.Show("yes");

     



  • Jean-Nicolas

    I am also a newbie on Interfaces.

    I see that the Array class implements the IList interface.  I want to use the IList.Contains method.  How do I reference and access it   "Contains" does not pop up in Intellisense.

    Thanks


  • rote

    Code reuse is accomplished by class inheritance, where you can have virtual methods and everything.

    In general, interfaces are used to say: "This class supports this behaviour", like IComparable if you need to order a list, or IEnumerable to iterate over it. That's why a lot of interfaces are very light-weight and only define one method.

    I guess it's ok to quote from a book here:
    "An abstract class serves as the base class for a family of derived classes, while interfaces are meant to be mixed in with other inheritance trees. [...] Inherting from an abstract base class implements the is-a relationship. Implementing an interface, on the other hand, defines a different relationship, called (not surprisingly) the implements relationship. These two relationships are subtly different. A car is-a vehicle, but it might implement the CanBeBoughtWithABigLoan capability (as can a house, for example)."
    (Taken from Programming C# by Jesse Liberty)

  • Nguyen Thai Hung

    One of the biggest benefits of using Interfaces (in my opinion) is to be able to "program to the Interface", as they say. Let me explain with a real-world example:

    Suppose I have created this Interface:



    public interface IFillFromFinder
    {
     void FillListView(DataSet dsList);
     void ClearListView();
    }

     

    Now, suppose I have a class that contains a ListView and implements the Interface:



    public class MySearchClass : MyUserControl, IFillFromFinder
    {
     #region Declarations

     protected ListView     oListView;
     protected MyFinderForm oFinder;

     #endregion

     #region Methods

     public void Search(ListView listView)
     {
      this.oListView = listview;
      this.ClearListView();

      this.oFinder = new MyFinderForm(this);
      oFinder.ShowDialog();
     }

     public void ClearListView()
     {
    put code here for clearing listview
     }

     public void FillListView(DataSet dsList)
     {
    put code here for filling listview
     }

     #endregion
    }

     


    As you can see, this class calls a Finder dialog Form, that gathers information from the User and performs a query against the backend data. If the Finder Dialog has been called from a control that implements this Interface (as in the above call from the Search() method), then it can also fill that control's ListView object by calling the methods on the Interface.

    Here's the relevant part of my Finder Dialog. Note that the oListControl is defined using the Interface. MyFinderForm needs to know absolutely nothing else about what's calling it, other than that it implements the IFillFromFinder interface.



    public class MyFinderForm : MyDialogForm 
    {
     #region Declarations

     public  MyListDataSet   oData = new MyListDataSet();
     protected IFillFromFinder    oListControl = null;

     #endregion

     #region Constructors

     public PremiseFinderForm(IFillFromFinder CallingControl)
     {   
      this.oListControl = CallingControl;   
      InitializeComponent();
     }  

     public PremiseFinderForm()  
     {   
      InitializeComponent();
     }

     #endregion
      
     #region Events

     private void cmdOK_Click(object sender, System.EventArgs e)
     {
      this.FillDataSet();


    If this dialog form was called by a class that implemented IFillFromFinder, then call call the Interface method to fill the ListView.
       
      if (this.oListControl != null)
      {
       if (this.chkReplace.Checked == true)
        this.oListControl.ClearListView();
       this.oListControl.FillListView(this.oData);
      }
     }


     #endregion
    }

     

    I hope this sort of helps explain interfaces by using a real world example.

     



  • Girish Lingappa

    Thanks.  Yes, I know about ArrayLists.  But in my situation, I do not need the other capabilities of ArrayList.  I was wanting to know about calling Interface methods.


  • Danielle01

    It's not so much reuse, but forcing certain behaviors. I think they call this programming to a contract.

    For example, I use interfaces in my integration (Quadrature) classes.

    In *my* code, I provide the logic to integrate any object that provides Evaluate() and Accumulate(). What happens in your object is entirely up to you, but you must provide those functions in order for the integration to work.

    More details at http://engineeringobjects.com/DotNet/Quadrature/Quadrature.htm

  • A newbie question on Interfaces