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

Y

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.
 
S

Samuel R. Neff

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
 
C

Chris

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.
 
C

Chris

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.
 
B

Bob Powell [MVP]

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.
 
C

Chris

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
 
C

Cor Ligthert

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
 
L

Larry Serflaten

you said:
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
 
Y

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top