473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1658

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: (Pete Cresswell) | last post by:
I've been perusing a "real-life" application and notice that for instance, in a button's Click() event, they don't write the processing code. Instead, they raise an event like AddNewRecord and...
11
by: milkyway | last post by:
Hello, I have an HTML page that I am trying to import 2 .js file (I created) into. These files are: row_functions.js and data_check_functions.js. Whenever I bring the contents of the files into...
5
by: J Allen Horner | last post by:
I'm just starting to learn C, and the tutorial I'm reading uses this code as an example: ----------- #include <stdio.h> #define MAX 10 int a; int rand_seed=10;
3
by: steve | last post by:
i'd like to inherit from a class while changing the access-level of an interface within the derived class. the below won't work...how do i properly do this...i don't want to expose the "add" method...
6
by: jason | last post by:
I found the below example online, while trying to under Interfaces. In layman's terms, can somebody explain what the purpose of this line is: void SportCharacteristics(); //LOCATED IN THE...
9
by: davetelling | last post by:
I am not a programmer, I'm an engineer trying to make an interface to a product I'm designing. I have used C# to make a form that interrogates the unit via the serial port and receives the data. I...
7
by: DJP | last post by:
Hi, I had sort of a noob question on memory allocation for strings. char *str1 = "Hello World!"; char str2 = "Hello World!"; In the above bit are both str1 & str2 stack allocated or heap...
5
by: Hydrogenfussion | last post by:
Hello and thank you for reading this. I have just started to learn C++ from www.cprogramming.com,its a great site! But i do not understand some of the terms. I am using Dev-C++. Can you tell me...
2
by: Carnell, James E | last post by:
I am thinking about purchasing a book, but wanted to make sure I could get through the code that implements what the book is about (Artificial Intelligence a Modern Approach). Anyway, I'm not a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.