469,925 Members | 1,546 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,925 developers. It's quick & easy.

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

you
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.
Nov 21 '05 #1
12 1492

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(district)
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 <yo*@me.com> wrote:
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?

Nov 21 '05 #2
"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."

http://msdn.microsoft.com/library/de...interinher.asp
"you" <yo*@me.com> wrote in message news:MP************************@news.microsoft.com ...
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.

Nov 21 '05 #3
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" <yo*@me.com> wrote in message
news:MP************************@news.microsoft.com ...
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.

Nov 21 '05 #4
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" <yo*@me.com> wrote in message
news:MP************************@news.microsoft.com ...
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.

Nov 21 '05 #5
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
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

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

"you" <yo*@me.com> wrote in message
news:MP************************@news.microsoft.com ...
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.

Nov 21 '05 #6
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

Nov 21 '05 #7
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

Nov 21 '05 #8
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

Nov 21 '05 #9

"you" <yo*@me.com> wrote
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?


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

Nov 21 '05 #10

"you" <yo*@me.com> wrote
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?


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

Nov 21 '05 #11
you
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
Nov 21 '05 #12
you
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
Nov 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by J Allen Horner | last post: by
3 posts views Thread by steve | last post: by
6 posts views Thread by jason | last post: by
9 posts views Thread by davetelling | last post: by
7 posts views Thread by DJP | last post: by
2 posts views Thread by Carnell, James E | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.