Why 'Interfaces'?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter N Roth

    #1

    Why 'Interfaces'?

    If i build a class in C#, i can declare a method abstract.
    If someone derives from this class, the abstract method
    must be overriden for the computation to succeed.

    ISTM that an Interface provides a similar service, i.e.,
    all methods must be implemented in order for the computation
    to succeed.

    Why use one or the other? Or maybe both?
    Is it because interfaces can be implemented in _any_
    language?

    --
    Grace + Peace,
    Peter N Roth
    Engineering Objects International




  • Wim Hollebrandse

    #2
    Re: Why 'Interfaces'?

    Inheritance follows the class hierarchies. And interface is a contract which
    describes what members should be included in the class which implements the
    interface. Interfaces cross class hierarchy boundaries. Also, a class can
    only inherit from one superclass, whereas a class can implement several
    interfaces.

    Hope that sums it up.

    Cheers,
    Wim Hollebrandse

    Zealand IT Ltd is a UK based IT consultancy and software development company that specialises in bespoke Microsoft .NET solutions including ASP.NET web applications and advanced SOA architectures for public and private sector clients. Please contact us for a free quote.



    "Peter N Roth" <refusing.spam@ mycompany.com> wrote in message
    news:#OD#ed3gDH A.1932@TK2MSFTN GP11.phx.gbl...[color=blue]
    > If i build a class in C#, i can declare a method abstract.
    > If someone derives from this class, the abstract method
    > must be overriden for the computation to succeed.
    >
    > ISTM that an Interface provides a similar service, i.e.,
    > all methods must be implemented in order for the computation
    > to succeed.
    >
    > Why use one or the other? Or maybe both?
    > Is it because interfaces can be implemented in _any_
    > language?
    >
    > --
    > Grace + Peace,
    > Peter N Roth
    > Engineering Objects International
    > http://engineeringobjects.com
    >
    >
    >[/color]


    Comment

    • Mr.Tickle

      #3
      Re: Why 'Interfaces'?

      C# has no multiple inheritance, yet it has multiple interfaces,

      What is the difference between having multiple interfaces and multiple base
      classes with regard to problems assosicated with multiple inheritance that
      is claimed why we have no multiple inheritance in C#.


      "Wim Hollebrandse" <wimATwimdows.c om> wrote in message
      news:#sMI0n3gDH A.408@TK2MSFTNG P10.phx.gbl...[color=blue]
      > Inheritance follows the class hierarchies. And interface is a contract[/color]
      which[color=blue]
      > describes what members should be included in the class which implements[/color]
      the[color=blue]
      > interface. Interfaces cross class hierarchy boundaries. Also, a class can
      > only inherit from one superclass, whereas a class can implement several
      > interfaces.
      >
      > Hope that sums it up.
      >
      > Cheers,
      > Wim Hollebrandse
      > http://www.wimdows.net
      > http://www.wimdows.com
      >
      >
      > "Peter N Roth" <refusing.spam@ mycompany.com> wrote in message
      > news:#OD#ed3gDH A.1932@TK2MSFTN GP11.phx.gbl...[color=green]
      > > If i build a class in C#, i can declare a method abstract.
      > > If someone derives from this class, the abstract method
      > > must be overriden for the computation to succeed.
      > >
      > > ISTM that an Interface provides a similar service, i.e.,
      > > all methods must be implemented in order for the computation
      > > to succeed.
      > >
      > > Why use one or the other? Or maybe both?
      > > Is it because interfaces can be implemented in _any_
      > > language?
      > >
      > > --
      > > Grace + Peace,
      > > Peter N Roth
      > > Engineering Objects International
      > > http://engineeringobjects.com
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Charlie Williams

        #4
        Why 'Interfaces'?

        An abstract class contains one or more abstract members.
        Therfore, you could design a 90% complete class leaving
        only one method undefined and requiring that non-abstract
        classes that derive from it include an implementation of
        the remaining method. Since .NET allows only single
        ineritence of classes, the abstract class you derive from
        will be the only class you derive from.

        Interfaces, on the other hand, only define member
        definitions. Classes that implement the interface must
        implement each member of that interface, but it is
        completely up the implementor how it is done.

        Any class that implements an interface can be cast to that
        interface. Therefore, it is possible to write methods
        that operate on an interface, without knowing at design
        time what kind of object you're going to be manipulating.

        For example, this is how the foreach statement works in
        C#. For a class to be used with the foreach statement, it
        must implement the interface IEnumerable. foreach uses
        the methods defined in this interface to step through the
        objects in your collection. Obviously, the person who
        wrote the foreach method for Microsoft didn't know
        anything about the BasketOfEggs class that you were going
        to write in the future, but if you implement IEnumerable
        (and IEnumerator) you can write a statement like -
        foreach(Egg egg in MyBasketOfEggs) . It does not matter
        what class Egg or BasketOfEggs derives from.

        Lastly (lastly for my post, not for the subject, which is
        much more expansive than I'm qualified to talk about),
        there is no limit to the number of interfaces a class can
        implement.

        Charlie


        [color=blue]
        >-----Original Message-----
        >If i build a class in C#, i can declare a method abstract.
        >If someone derives from this class, the abstract method
        >must be overriden for the computation to succeed.
        >
        >ISTM that an Interface provides a similar service, i.e.,
        >all methods must be implemented in order for the[/color]
        computation[color=blue]
        >to succeed.
        >
        >Why use one or the other? Or maybe both?
        >Is it because interfaces can be implemented in _any_
        >language?
        >
        >--
        >Grace + Peace,
        >Peter N Roth
        >Engineering Objects International
        >http://engineeringobjects.com
        >
        >
        >
        >.
        >[/color]

        Comment

        • Frank Oquendo

          #5
          Re: Why 'Interfaces'?

          Thus spake Peter N Roth:
          [color=blue]
          > Why use one or the other? Or maybe both?[/color]

          An interface is typically used where seemingly common functionality is
          required for different types of objects. Since C# only allows single
          inheritance, you would have to move all common abstract methods into a
          single base from which all other objects implementing those methods must
          derive. There are many scenarios where this would be highly inefficient
          and needlessly complex.

          Interfaces allow us to get around the problem in much the same way but
          without inheritance. If you take a look at an existing interface such as
          IList, you will see that it is implemented in 44 widely disparate .NET
          classes. As such, the funcionality provided by IList can be used in a
          uniform manner from any class which implements it.

          Now imagine trying to develop all 44 of those classes if they had to
          descend from a single base class. Interfaces make it easy to design
          objects with no common ancestor and common functionality.

          --
          There are 10 kinds of people. Those who understand binary and those who
          don't.




          Comment

          • Daniel Pratt

            #6
            Re: Why 'Interfaces'?

            Hi Mr. Tickle,

            "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
            news:u4z7yv3gDH A.1088@TK2MSFTN GP10.phx.gbl...[color=blue]
            > C# has no multiple inheritance, yet it has multiple interfaces,
            >
            > What is the difference between having multiple interfaces and multiple[/color]
            base[color=blue]
            > classes with regard to problems assosicated with multiple inheritance that
            > is claimed why we have no multiple inheritance in C#.[/color]


            Consider this example:

            public class Foo
            {
            ...
            public override string ToString()
            {
            return "Foo";
            }
            ...
            }
            ...
            public class Bar
            {
            ...
            public override string ToString()
            {
            return "Bar";
            }
            ...
            }

            // Let's pretend we have multiple inheritance
            public class FooBar : Foo, Bar
            {
            ...
            }

            Now assuming class FooBar does not implement "ToString" itself, which
            implementation of "ToString" should it get, the one from "Foo" or the one
            from "Bar"? If this were C++, FooBar would inherit Foo's version of
            "ToString" since Foo comes first in the inheritance list. Not very
            intuitive, IMO.

            The above illustrates the primary *technical* problem with MI, but there
            are non-technical problems with MI as well. Basically, it's one of those
            things that, in practice, often leads to "yucky" code.

            My $0.02.

            Regards,
            Dan


            Comment

            • Mr.Tickle

              #7
              Re: Why 'Interfaces'?

              how does that differ from Multiple Interfaces then. that are permitted in C#


              "Daniel Pratt" <kolREMOVETHISk ata_is@hotmail. com> wrote in message
              news:#zk80a5gDH A.884@TK2MSFTNG P12.phx.gbl...[color=blue]
              > Hi Mr. Tickle,
              >
              > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
              > news:u4z7yv3gDH A.1088@TK2MSFTN GP10.phx.gbl...[color=green]
              > > C# has no multiple inheritance, yet it has multiple interfaces,
              > >
              > > What is the difference between having multiple interfaces and multiple[/color]
              > base[color=green]
              > > classes with regard to problems assosicated with multiple inheritance[/color][/color]
              that[color=blue][color=green]
              > > is claimed why we have no multiple inheritance in C#.[/color]
              >
              >
              > Consider this example:
              >
              > public class Foo
              > {
              > ...
              > public override string ToString()
              > {
              > return "Foo";
              > }
              > ...
              > }
              > ...
              > public class Bar
              > {
              > ...
              > public override string ToString()
              > {
              > return "Bar";
              > }
              > ...
              > }
              >
              > // Let's pretend we have multiple inheritance
              > public class FooBar : Foo, Bar
              > {
              > ...
              > }
              >
              > Now assuming class FooBar does not implement "ToString" itself, which
              > implementation of "ToString" should it get, the one from "Foo" or the one
              > from "Bar"? If this were C++, FooBar would inherit Foo's version of
              > "ToString" since Foo comes first in the inheritance list. Not very
              > intuitive, IMO.
              >
              > The above illustrates the primary *technical* problem with MI, but[/color]
              there[color=blue]
              > are non-technical problems with MI as well. Basically, it's one of those
              > things that, in practice, often leads to "yucky" code.
              >
              > My $0.02.
              >
              > Regards,
              > Dan
              >
              >[/color]


              Comment

              • 100

                #8
                Re: Why 'Interfaces'?

                Hi Daniel,

                See my comments inline.
                [color=blue]
                > Consider this example:
                >
                > public class Foo
                > {
                > ...
                > public override string ToString()
                > {
                > return "Foo";
                > }
                > ...
                > }
                > ...
                > public class Bar
                > {
                > ...
                > public override string ToString()
                > {
                > return "Bar";
                > }
                > ...
                > }
                >
                > // Let's pretend we have multiple inheritance
                > public class FooBar : Foo, Bar
                > {
                > ...
                > }
                >
                > Now assuming class FooBar does not implement "ToString" itself, which
                > implementation of "ToString" should it get, the one from "Foo" or the one
                > from "Bar"? If this were C++, FooBar would inherit Foo's version of
                > "ToString" since Foo comes first in the inheritance list. Not very
                > intuitive, IMO.[/color]

                Actually calling ToString for pointer or reference of type FooBar is error
                in C++ anyway. The program won't compile.

                You have to cast to one of the base classes.

                FooBar fb = new FooBar();
                ((Foo*)fb)->ToString();
                or one can override ToString in FooBar and call the ToString method of the
                base class of choice.

                This is in case that the classes don't implicitly inherit from a common
                class (Object)

                If we have this Object class in this case programmers have two choices to
                use virtual or non-virtual inheritance.
                Anyway the laguage has standard constructions to resolve any ambiguities.


                [color=blue]
                > The above illustrates the primary *technical* problem with MI, but there
                > are non-technical problems with MI as well. Basically, it's one of those
                > things that, in practice, often leads to "yucky" code.[/color]

                How "yucky" is the code depends on how good is the design. I can write
                really "yucky" code without using multiple inheritance ;)))))

                Even though multipile inheritence needs to be used carefully it is verry
                useful and I miss it once in a while.

                However absence of multiple inheritance in .NET gives the JIT opportunity to
                optimize the code better, speed up all safety checkings and so on.

                B\rgds
                100


                Comment

                • Arthur Mnev

                  #9
                  Re: Why 'Interfaces'?

                  An ineterface is a "contract" between two objects - it defines members
                  through which you are accessing functionality; a parent class,
                  however, defines a good portion of functionality on its own. Ofcourse
                  you could implement a class comprised of virutal methods (and in times
                  you do need that specific functionality).

                  In general you would use an abstract class to define some
                  functionality that requires some additional enhancements by inherited
                  class.
                  In case of interfaces, however, you would specify access methodology
                  to a specific class.

                  One of a good examples where you'd use interfaces is version control
                  of the code or unknown parameter to a function that required to
                  support only a specific set of functions.


                  a small example where interfaces are nearly unavoidable.

                  - you have an object that supplies data
                  - object is submitted from a client to a server (who cares what the
                  transport is)

                  public interface IMyDataSource
                  {
                  GetData();
                  }

                  Server has a function defined

                  public static void DataProcessor(I MyDataSource client_data)
                  {
                  //process data
                  }

                  if you need other applications to be able to supply data to you, all
                  you need to tell a developer is to implement IMyDataSource interface
                  and send you the object.

                  Imagine that some time after you have different version of the
                  function performing the same thing. You cant remove the current
                  funciton since you have 1000 clients out and God only knows how many
                  other apps implementing IMyDataSource interface.

                  What you do is:

                  public interface IMyDataSource_v 2
                  {
                  GetData();
                  }

                  public static void DataProcessor(I MyDataSource_v2 client_data)
                  {
                  //process data
                  }

                  Now, those clients that implement second version of the client app
                  will get processed by the second function, and the first group will
                  not break.

                  If you were to use abstract class with GetData() method implemented,
                  you would be stuck (overloads could have helped but it would be very
                  messy)

                  "Peter N Roth" <refusing.spam@ mycompany.com> wrote in message news:<#OD#ed3gD HA.1932@TK2MSFT NGP11.phx.gbl>. ..[color=blue]
                  > If i build a class in C#, i can declare a method abstract.
                  > If someone derives from this class, the abstract method
                  > must be overriden for the computation to succeed.
                  >
                  > ISTM that an Interface provides a similar service, i.e.,
                  > all methods must be implemented in order for the computation
                  > to succeed.
                  >
                  > Why use one or the other? Or maybe both?
                  > Is it because interfaces can be implemented in _any_
                  > language?[/color]

                  Comment

                  • Jeff Louie

                    #10
                    Re: Why 'Interfaces'?

                    This may help also:

                    Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


                    Regards,
                    Jeff

                    *** Sent via Developersdex http://www.developersdex.com ***
                    Don't just participate in USENET...get rewarded for it!

                    Comment

                    • Daniel Pratt

                      #11
                      Re: Why 'Interfaces'?

                      Hi 100,

                      "100" <100@100.com> wrote in message
                      news:OK87nR6gDH A.884@TK2MSFTNG P12.phx.gbl...[color=blue]
                      > Hi Daniel,[/color]
                      <snip>[color=blue]
                      >
                      > Actually calling ToString for pointer or reference of type FooBar is[/color]
                      error[color=blue]
                      > in C++ anyway. The program won't compile.
                      >
                      > You have to cast to one of the base classes.
                      >
                      > FooBar fb = new FooBar();
                      > ((Foo*)fb)->ToString();
                      > or one can override ToString in FooBar and call the ToString method of the
                      > base class of choice.
                      >
                      > This is in case that the classes don't implicitly inherit from a common
                      > class (Object)[/color]

                      In my example both Foo and Bar *do* inherit from the same base class
                      (Object). Hence the ToString implementations are overrides. And if you cast
                      to the base class and call ToString, then the compiler will have to choose
                      either the Foo or Bar implementation of ToString. Even though the behavior
                      is predictable, it still seems unnatural (to me) that a significant behavior
                      change could result from simply re-ordering the inheritance list.
                      [color=blue]
                      > If we have this Object class in this case programmers have two choices to
                      > use virtual or non-virtual inheritance.
                      > Anyway the laguage has standard constructions to resolve any ambiguities.[/color]

                      You know, this brings up something I hadn't even thought of before. To
                      virtually inherit or not and the implications of that choice on behavior and
                      performance.

                      <snip>[color=blue]
                      > How "yucky" is the code depends on how good is the design. I can write
                      > really "yucky" code without using multiple inheritance ;)))))[/color]

                      Agreed.
                      [color=blue]
                      > Even though multipile inheritence needs to be used carefully it is verry
                      > useful and I miss it once in a while.[/color]

                      I'll admit I'm torn on this subject. I've never been a fan of the idea
                      of removing or preventing something in a language just because *some* people
                      will misuse/abuse it. There are also times when I think it would be nice to
                      use MI. On the other hand, I appreciate a "clean", straightforward language
                      that encourages me to do "the right thing". To me, MI seems counter to those
                      ideals.

                      To summarize, I don't think MI is bad or useless. I do think it's a lot
                      less trivial than many people realize.

                      Regards,
                      Dan


                      Comment

                      • Peter N Roth

                        #12
                        Re: Why 'Interfaces'?

                        How is an IEgg better than a CEgg, where CEgg is
                        an abstract class? I should be able to cast a CEgg
                        derivative to CEgg, so the superiority of one over
                        the other is not clear to me.

                        Unless this somehow goes across languages? ie, I
                        define an IEgg in C# and a Delphi or Eiffel
                        programmer can implement it and use my EggBasket.
                        --
                        Grace + Peace,
                        Peter N Roth
                        Engineering Objects International



                        "Charlie Williams" <cwilliams1145@ hotmail.com> wrote in message
                        news:26c901c383 7d$f3d74d30$a40 1280a@phx.gbl.. .[color=blue]
                        > An abstract class contains one or more abstract members.
                        > Therfore, you could design a 90% complete class leaving
                        > only one method undefined and requiring that non-abstract
                        > classes that derive from it include an implementation of
                        > the remaining method. Since .NET allows only single
                        > ineritence of classes, the abstract class you derive from
                        > will be the only class you derive from.
                        >
                        > Interfaces, on the other hand, only define member
                        > definitions. Classes that implement the interface must
                        > implement each member of that interface, but it is
                        > completely up the implementor how it is done.
                        >
                        > Any class that implements an interface can be cast to that
                        > interface. Therefore, it is possible to write methods
                        > that operate on an interface, without knowing at design
                        > time what kind of object you're going to be manipulating.
                        >
                        > For example, this is how the foreach statement works in
                        > C#. For a class to be used with the foreach statement, it
                        > must implement the interface IEnumerable. foreach uses
                        > the methods defined in this interface to step through the
                        > objects in your collection. Obviously, the person who
                        > wrote the foreach method for Microsoft didn't know
                        > anything about the BasketOfEggs class that you were going
                        > to write in the future, but if you implement IEnumerable
                        > (and IEnumerator) you can write a statement like -
                        > foreach(Egg egg in MyBasketOfEggs) . It does not matter
                        > what class Egg or BasketOfEggs derives from.
                        >
                        > Lastly (lastly for my post, not for the subject, which is
                        > much more expansive than I'm qualified to talk about),
                        > there is no limit to the number of interfaces a class can
                        > implement.
                        >
                        > Charlie
                        >
                        >
                        >[color=green]
                        > >-----Original Message-----
                        > >If i build a class in C#, i can declare a method abstract.
                        > >If someone derives from this class, the abstract method
                        > >must be overriden for the computation to succeed.
                        > >
                        > >ISTM that an Interface provides a similar service, i.e.,
                        > >all methods must be implemented in order for the[/color]
                        > computation[color=green]
                        > >to succeed.
                        > >
                        > >Why use one or the other? Or maybe both?
                        > >Is it because interfaces can be implemented in _any_
                        > >language?
                        > >
                        > >--
                        > >Grace + Peace,
                        > >Peter N Roth
                        > >Engineering Objects International
                        > >http://engineeringobjects.com
                        > >
                        > >
                        > >
                        > >.
                        > >[/color][/color]


                        Comment

                        • news.verizon.net

                          #13
                          Re: Why 'Interfaces'?

                          Interesting page, a nice clarification.

                          Ideally, then, your development process
                          includes a 'revise' step such that you examine
                          your base classes for re-publication as Interfaces?

                          The 'contract' example recalls the Design by Contract
                          idea of Bertrand Meyers - "I will eventually get
                          around to reading that book, too!," I lied.

                          --
                          Grace + Peace,
                          Peter N Roth
                          Engineering Objects International



                          "Jeff Louie" <jeff_louie@yah oo.com> wrote in message
                          news:uKz9M$8gDH A.1668@TK2MSFTN GP12.phx.gbl...[color=blue]
                          > This may help also:
                          >
                          > http://www.geocities.com/jeff_louie/OOP/oop9.htm
                          >
                          > Regards,
                          > Jeff
                          >
                          > *** Sent via Developersdex http://www.developersdex.com ***
                          > Don't just participate in USENET...get rewarded for it![/color]


                          Comment

                          Working...