Interface Implementation

Last post 12-02-2008 7:56 AM by JeffreyABecker. 5 replies.

Sort Posts:

  • Interface Implementation

    11-29-2008, 5:39 AM
    • Member
      1 point Member
    • szeeshu
    • Member since 03-21-2007, 5:45 PM
    • Posts 7

    In oops we can't give implementation of interfaces rather we just allowed to give signature i.e. declaration only. Now my question is that we have interfaces like IEnumerator, IDisposible etc we use their methods like

    IEnumerator ie = list.GetEnumerator();

    ie.GetNext();

    so how Interface method like GetNext() definition is defined here?

     

     

  • Re: Interface Implementation

    11-29-2008, 9:17 AM
    Answer
    • Member
      398 point Member
    • Kamarikan
    • Member since 02-15-2007, 9:38 AM
    • Tehran
    • Posts 98

    it's based on what ypu need and based on your need.

    the interface is a logic e.g all of the car in whole world should put the  speed pedal on right side of the foot , any body can changethe logic . each car company should implement this interface . these interfaces type became like a law , and any body can changed it , if one wanted to change it he should first change the whole world people mind to changing speed pedal to another side .

    but some of interfaces bavaive based on your application like you want to implement a ienumerable interface and you want to implement getEnum... I can implemenmt it in my needs and you should implement it asyou want , there is no limitation in implementing interfaces

     

    look at one sample here : http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx

    please let me know whats happen to your aplication after this information . and mark this as answer if the answer was right.

     

    Nasser Hajloo
    Tehran , Iran , Middle east
    Filed under:
  • Re: Interface Implementation

    12-01-2008, 8:32 AM
    • Member
      1 point Member
    • szeeshu
    • Member since 03-21-2007, 5:45 PM
    • Posts 7

    My question was that, If I create Interface like

    interface ISetter { public SetValue(object); } 

    I am not allowed to give implmentation inside interface rather I can just give its defination in inherited class.

    but If we look on interfaces like IEnumerator, IDisposible, IList etc their methods are pre-implemented and we can directly access them by

    Interface.MethodName(). eg. IEnumerator.GetNext(); How it is possible?

     

     

     

     

  • Re: Interface Implementation

    12-01-2008, 9:54 AM
    • All-Star
      91,992 point All-Star
    • SGWellens
    • Member since 01-02-2007, 9:27 PM
    • Twin Cities, MN
    • Posts 7,505
    • Moderator
      TrustedFriends-MVPs

    The methods are not implemented, the signature of the methods are implemented:

    If you look at IEnumberator you see the following and note the methods are empty shells.

    namespace System.Collections
    {
        // Summary:
        //     Supports a simple iteration over a nongeneric collection.
        [ComVisible(true)]
        [Guid("496B0ABF-CDEE-11d3-88E8-00902754C43A")]
        public interface IEnumerator
        {
            // Summary:
            //     Gets the current element in the collection.
            //
            // Returns:
            //     The current element in the collection.
            //
            // Exceptions:
            //   System.InvalidOperationException:
            //     The enumerator is positioned before the first element of the collection or
            //     after the last element.
            object Current { get; }
    
            // Summary:
            //     Advances the enumerator to the next element of the collection.
            //
            // Returns:
            //     true if the enumerator was successfully advanced to the next element; false
            //     if the enumerator has passed the end of the collection.
            //
            // Exceptions:
            //   System.InvalidOperationException:
            //     The collection was modified after the enumerator was created.
            bool MoveNext();
            //
            // Summary:
            //     Sets the enumerator to its initial position, which is before the first element
            //     in the collection.
            //
            // Exceptions:
            //   System.InvalidOperationException:
            //     The collection was modified after the enumerator was created.
            void Reset();
        }
    }
    
     
    Steve Wellens

    My blog
  • Re: Interface Implementation

    12-02-2008, 2:04 AM
    • Member
      1 point Member
    • szeeshu
    • Member since 03-21-2007, 5:45 PM
    • Posts 7

    ya i've look at IEnumerator interface definition, its methods are empty however i can use without overriding them cause some where internally its methods definition is hidden but where ? Like

    IEnumerator ie = Object.GetType().GetMethods();

    while(ie.GetNext())

    {

     ie.Current; 

     I haven't done any thing just use its method GetNext() and property Current. I want to know where its implementation is hidden?  so  that i can  make interface whose methods are pre-implemented and i can use its methods just by giving InterfaceObject.MethodName(). I hope now you will understand my point of view.
     

  • Re: Interface Implementation

    12-02-2008, 7:56 AM
    • Star
      14,298 point Star
    • JeffreyABecker
    • Member since 10-04-2004, 4:27 AM
    • Philadelphia, PA
    • Posts 2,915

     I think you're confused.  Theres a difference between the interface IEnumerable and a class which implements IEnumerable.  In the example you give, you're really accessing the implementation defined by a class which implements IEnumerable.  To do what you want to do, use an abstract class. 

Page 1 of 1 (6 items)