NOOB: Could someone explain in laymen's terms the reason for an Interface?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • you

    NOOB: Could someone explain in laymen's terms the reason for an Interface?

    I may be just stupid, but I don't understand the point of using an
    Interface.


    I am going through a couple of books at the moment trying to teach
    myself a little vb.net. Just for the heck of it, ya know. Kinda
    interesting stuff. Anyhoo; Both of these talk about what an interface
    is, give an example, and then happily go on their merry way leaving me
    to guess as to WHY you would create and use one?

    How is;

    Public Interface IJunk
    Sub MoreJunk()
    End Interface

    Public Class UseJunk
    Implements IJunk
    Public Sub MoreJunk() Implements Ijunk.MoreJunk
    End Class


    Any different than Just;

    Public Class UseJunk
    Public Sub MoreJunk()
    End Class

    ????

    Other than more typing that is.
  • Samuel R. Neff

    #2
    Re: NOOB: Could someone explain in laymen's terms the reason for an Interface?


    Here's one situation where I just used an interface (literally like 10
    minutes ago).

    I'm migrating code from VB6 to VB.NET and the code builds a tree of
    school district names. The tree is usually just one level but
    sometimes it's two levels where we group districts. The old VB6 code
    looked basically like this;

    for each district
    ... if grouped then
    ... .. add to group
    ... else
    ... .. add directly to root
    ... end if
    next

    but this is a waiste of cpu since the if is inside the loop. To solve
    this in .NET I used an interface (which I could have done in VB6 too,
    but I hadn't read GOF then).

    VB.NET code:

    dim filler as IFiller
    if grouped then
    ... filler = new GroupedFiller()
    else
    ... filler = new PlainFiller()
    end if

    for each district
    ... filler.Add(dist rict)
    next

    and I have

    interface IFiller
    class GroupedFiller implements IFiller
    class PlainFiller implements IFiller

    This way, the code code above can act on an instance of IFiller the
    same way regardless of whether the actual instance is one of
    GroupedFiller or PlainFiller.

    HTH,

    Sam


    On Fri, 7 Jan 2005 13:34:33 -0600, you <you@me.com> wrote:
    [color=blue]
    >I may be just stupid, but I don't understand the point of using an
    >Interface.
    >
    >
    >I am going through a couple of books at the moment trying to teach
    >myself a little vb.net. Just for the heck of it, ya know. Kinda
    >interesting stuff. Anyhoo; Both of these talk about what an interface
    >is, give an example, and then happily go on their merry way leaving me
    >to guess as to WHY you would create and use one?
    >[/color]

    Comment

    • Darious Snell

      #3
      Re: Could someone explain in laymen's terms the reason for an Interface?

      "This is what interfaces provide: a way to inherit just a set of method and property
      specifications with no implementation to worry about and no problem inheriting from as
      many interfaces as you need."




      "you" <you@me.com> wrote in message news:MPG.1c48a0 47148a7cf398968 2@news.microsof t.com...[color=blue]
      > I may be just stupid, but I don't understand the point of using an
      > Interface.
      >
      >
      > I am going through a couple of books at the moment trying to teach
      > myself a little vb.net. Just for the heck of it, ya know. Kinda
      > interesting stuff. Anyhoo; Both of these talk about what an interface
      > is, give an example, and then happily go on their merry way leaving me
      > to guess as to WHY you would create and use one?
      >
      > How is;
      >
      > Public Interface IJunk
      > Sub MoreJunk()
      > End Interface
      >
      > Public Class UseJunk
      > Implements IJunk
      > Public Sub MoreJunk() Implements Ijunk.MoreJunk
      > End Class
      >
      >
      > Any different than Just;
      >
      > Public Class UseJunk
      > Public Sub MoreJunk()
      > End Class
      >
      > ????
      >
      > Other than more typing that is.[/color]


      Comment

      • Chris

        #4
        Re: Could someone explain in laymen's terms the reason for an Interface?

        think of an interface as a class template.

        it allows you to make sure all classes that use the interface are created
        in a consistent fashion.

        you are indicating with an interface that the class behave in a particular
        way, but unlike inheritance, the class itself will provide the
        implementation.

        check out the ICloneable, IComparable, IDisposable interfaces in help for
        some good real world uses.

        "you" <you@me.com> wrote in message
        news:MPG.1c48a0 47148a7cf398968 2@news.microsof t.com...[color=blue]
        > I may be just stupid, but I don't understand the point of using an
        > Interface.
        >
        >
        > I am going through a couple of books at the moment trying to teach
        > myself a little vb.net. Just for the heck of it, ya know. Kinda
        > interesting stuff. Anyhoo; Both of these talk about what an interface
        > is, give an example, and then happily go on their merry way leaving me
        > to guess as to WHY you would create and use one?
        >
        > How is;
        >
        > Public Interface IJunk
        > Sub MoreJunk()
        > End Interface
        >
        > Public Class UseJunk
        > Implements IJunk
        > Public Sub MoreJunk() Implements Ijunk.MoreJunk
        > End Class
        >
        >
        > Any different than Just;
        >
        > Public Class UseJunk
        > Public Sub MoreJunk()
        > End Class
        >
        > ????
        >
        > Other than more typing that is.[/color]


        Comment

        • Chris

          #5
          Re: Could someone explain in laymen's terms the reason for an Interface?

          think of an interface as a class template.

          it allows you to make sure all classes that use the interface are created
          in a consistent fashion.

          you are indicating with an interface that the class behave in a particular
          way, but unlike inheritance, the class itself will provide the
          implementation.

          check out the ICloneable, IComparable, IDisposable interfaces in help for
          some good real world uses.

          "you" <you@me.com> wrote in message
          news:MPG.1c48a0 47148a7cf398968 2@news.microsof t.com...[color=blue]
          > I may be just stupid, but I don't understand the point of using an
          > Interface.
          >
          >
          > I am going through a couple of books at the moment trying to teach
          > myself a little vb.net. Just for the heck of it, ya know. Kinda
          > interesting stuff. Anyhoo; Both of these talk about what an interface
          > is, give an example, and then happily go on their merry way leaving me
          > to guess as to WHY you would create and use one?
          >
          > How is;
          >
          > Public Interface IJunk
          > Sub MoreJunk()
          > End Interface
          >
          > Public Class UseJunk
          > Implements IJunk
          > Public Sub MoreJunk() Implements Ijunk.MoreJunk
          > End Class
          >
          >
          > Any different than Just;
          >
          > Public Class UseJunk
          > Public Sub MoreJunk()
          > End Class
          >
          > ????
          >
          > Other than more typing that is.[/color]


          Comment

          • Bob Powell [MVP]

            #6
            Re: Could someone explain in laymen's terms the reason for an Interface?

            An interface provides a contract for how an object will present itself to
            the outside world.

            This is very useful in an object oriented system where objects may be
            derived from many different base-classes. Each of the bases have their own
            behaviours and don't necessarily have the same capabilities as the next
            object. If however you wanted to make sure that all objects could do the
            same thing no matter what their ancestry you can use an interface.

            Consider the situation where you wanted to create an extensible
            architecture. One method is to create a base class and force everyone who
            want's to make a plug-in derive their objects from that base. Another is to
            provide an interface, say IPluggable, and tell everyone that the only thing
            you need to do to make a plugin is to implement IPluggable.

            The interface method is way more flexible because you don't impose any
            implementation baggage on the architecture and all you specify is how the
            object should appear to behave.

            --
            Bob Powell [MVP]
            Visual C#, System.Drawing

            Find great Windows Forms articles in Windows Forms Tips and Tricks


            Answer those GDI+ questions with the GDI+ FAQ


            All new articles provide code in C# and VB.NET.
            Subscribe to the RSS feeds provided and never miss a new article.





            "you" <you@me.com> wrote in message
            news:MPG.1c48a0 47148a7cf398968 2@news.microsof t.com...[color=blue]
            >I may be just stupid, but I don't understand the point of using an
            > Interface.
            >
            >
            > I am going through a couple of books at the moment trying to teach
            > myself a little vb.net. Just for the heck of it, ya know. Kinda
            > interesting stuff. Anyhoo; Both of these talk about what an interface
            > is, give an example, and then happily go on their merry way leaving me
            > to guess as to WHY you would create and use one?
            >
            > How is;
            >
            > Public Interface IJunk
            > Sub MoreJunk()
            > End Interface
            >
            > Public Class UseJunk
            > Implements IJunk
            > Public Sub MoreJunk() Implements Ijunk.MoreJunk
            > End Class
            >
            >
            > Any different than Just;
            >
            > Public Class UseJunk
            > Public Sub MoreJunk()
            > End Class
            >
            > ????
            >
            > Other than more typing that is.[/color]


            Comment

            • Chris

              #7
              Re: NOOB: Could someone explain in laymen's terms the reason for an Interface?

              Have you ever used Adobe Photoshop? or any graphics app that has
              "plugins"?

              Plugins for these apps are typically written by third parties but in
              order to work correctly in Photoshop, they must be written in a certain
              way.

              Photoshop, then, would define the interface, that is a specification
              for a list of functions and properties that the plugin author must use,
              or implement.

              As long as the plugin writer follows the interface, then Photoshop will
              be able use the plugin. Adobe has no idea about how the plugin works,
              it just knows that it can call certain methods and get certain data
              back in return. In other words, Photoshop doesn't have to know about
              the internal workings of the plugin.

              By the same token, the plugin doesn't have to know about the internal
              workings of Photoshop either. All it knows is that Photoshop will call
              a certain method and pass in certain data. The plugin then takes that
              data, manipulates it however it wants, and returns data back to
              Photoshop. What it returns is defined by the interface.

              The whole point is that, Adobe publishes the interface, and third party
              developers can develop to that interface with little or no help from
              Adobe.

              Chris

              Comment

              • Cor Ligthert

                #8
                Re: Could someone explain in laymen's terms the reason for an Interface?

                you,

                To tell the same as all others with other words.

                An interface tells exactly what should be at least in a class to be confirm
                that interface.

                Cor



                Comment

                • Cor Ligthert

                  #9
                  Re: Could someone explain in laymen's terms the reason for an Interface?

                  you,

                  To tell the same as all others with other words.

                  An interface tells exactly what should be at least in a class to be confirm
                  that interface.

                  Cor



                  Comment

                  • Larry Serflaten

                    #10
                    Re: Could someone explain in laymen's terms the reason for an Interface?


                    "you" <you@me.com> wrote[color=blue]
                    > I am going through a couple of books at the moment trying to teach
                    > myself a little vb.net. Just for the heck of it, ya know. Kinda
                    > interesting stuff. Anyhoo; Both of these talk about what an interface
                    > is, give an example, and then happily go on their merry way leaving me
                    > to guess as to WHY you would create and use one?[/color]

                    In short, an interface is a connection point in software, much like the
                    modem jack, or keyboard jack are for hardware. The RJ11 (or which ever)
                    jack the telephone uses is standardized so that anything that responds like
                    a telephone can use the jack. Because the jack is standard, many others can
                    build on to the telephone network (Modems, Faxes, Telephones, etc.) and
                    can add their own features to whatever devices they build.

                    The same for the electrical wall outlet. The whole house has electricity,
                    but the interface is just the 3-pronged plug (in the U.S.) which any device
                    can use. That type of plug indicates there is about 120V of AC (60 Hz)
                    available. Anybody who builds a device can plan on using 120V AC through
                    that type of interface. Washers, dryers, and funaces may use 240V AC
                    and they have a different interface (a different style of outlet).

                    Having such a connection point allows for later mixing and matching between
                    major pieces of the whole. Whether that is different devices in your house,
                    or different software for your computer.

                    LFS



                    Comment

                    • Larry Serflaten

                      #11
                      Re: Could someone explain in laymen's terms the reason for an Interface?


                      "you" <you@me.com> wrote[color=blue]
                      > I am going through a couple of books at the moment trying to teach
                      > myself a little vb.net. Just for the heck of it, ya know. Kinda
                      > interesting stuff. Anyhoo; Both of these talk about what an interface
                      > is, give an example, and then happily go on their merry way leaving me
                      > to guess as to WHY you would create and use one?[/color]

                      In short, an interface is a connection point in software, much like the
                      modem jack, or keyboard jack are for hardware. The RJ11 (or which ever)
                      jack the telephone uses is standardized so that anything that responds like
                      a telephone can use the jack. Because the jack is standard, many others can
                      build on to the telephone network (Modems, Faxes, Telephones, etc.) and
                      can add their own features to whatever devices they build.

                      The same for the electrical wall outlet. The whole house has electricity,
                      but the interface is just the 3-pronged plug (in the U.S.) which any device
                      can use. That type of plug indicates there is about 120V of AC (60 Hz)
                      available. Anybody who builds a device can plan on using 120V AC through
                      that type of interface. Washers, dryers, and funaces may use 240V AC
                      and they have a different interface (a different style of outlet).

                      Having such a connection point allows for later mixing and matching between
                      major pieces of the whole. Whether that is different devices in your house,
                      or different software for your computer.

                      LFS



                      Comment

                      • you

                        #12
                        Re: NOOB: Could someone explain in laymen's terms the reason for an Interface?

                        Heya all,
                        Thanks for the definitions. I beleive that I understand it alot better
                        now. I was under the impression that an interface was something that I
                        had to make (opposed to just making a class) in order to do things
                        correctly. Which made it seem like a redundant and unnecessary step.
                        Then again, this is a noob talkin'.

                        Thanks again!

                        Jason

                        Comment

                        • you

                          #13
                          Re: NOOB: Could someone explain in laymen's terms the reason for an Interface?

                          Heya all,
                          Thanks for the definitions. I beleive that I understand it alot better
                          now. I was under the impression that an interface was something that I
                          had to make (opposed to just making a class) in order to do things
                          correctly. Which made it seem like a redundant and unnecessary step.
                          Then again, this is a noob talkin'.

                          Thanks again!

                          Jason

                          Comment

                          Working...