473,748 Members | 7,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to understand the purpose of interfaces

jm
I am having trouble understanding the purposes of an interface, even
though the concept of interfaces is around me all the time (user
interface, for example). I'm just not understanding software
interfaces.

Like anything else, it appears Interfaces are by design something that
requires documentation. I know this may be obvious, but if I have a
class A and I say, well, class A implements IMyInterface. The fact
that it implements IMyInterface is supposed to mean something to me. I
am guessing that MS and other Sun have a lot of well defined and
documented interfaces that I would eventually care about.

I guess I am looking for a time when their absence would require them.
I think that by seeing their missing from a given situation that
requires them (or would be hard without them), I might see and
understand their necessity and understand.

Does anyone have an example of this? I hope this made some sense.

Thank you.

Nov 3 '06 #1
27 3858
"jm" <ne***********@ gmail.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
if I have a
class A and I say, well, class A implements IMyInterface. The fact
that it implements IMyInterface is supposed to mean something to me.
What it means is that you can use an object of that class where an object of
type IMyInterface is expected.

Otherwise, it just means that IMyInterface methods/properties are
implemented in the class, but you wouldn't need to use an interface for that
purpose alone.

//ark
Nov 3 '06 #2
Hello JM,

Have discussed recently. See there http://petesbloggerama.blogspot.com/...nterfaces.html

JI am having trouble understanding the purposes of an interface, even
Jthough the concept of interfaces is around me all the time (user
Jinterface, for example). I'm just not understanding software
Jinterfaces.
J>
JLike anything else, it appears Interfaces are by design something
Jthat requires documentation. I know this may be obvious, but if I
Jhave a class A and I say, well, class A implements IMyInterface. The
Jfact that it implements IMyInterface is supposed to mean something to
Jme. I am guessing that MS and other Sun have a lot of well defined
Jand documented interfaces that I would eventually care about.
J>
JI guess I am looking for a time when their absence would require
Jthem. I think that by seeing their missing from a given situation
Jthat requires them (or would be hard without them), I might see and
Junderstand their necessity and understand.
J>
JDoes anyone have an example of this? I hope this made some sense.
J>
JThank you.
J>
---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Nov 3 '06 #3
jm... Just look at an interface as a contract. Wherever a software
contract would
be useful consider writing an interface. An example is a network
service. You
program to the interface ISomeUsefulNetw orkService. Any client can look
up
your service on the network and interact with your particular
implementation of
ISomeUsefulNetw orkService as long as you implement
ISomeUsefulNetw orkService. Your competitors can also implement their own
network service that implements this interface. To the client, the
actual
implementation details are hidden. The client simply makes calls to the
interface
and can utilize any network service that implements the interface.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 4 '06 #4
Hi jm,
here is a good example, keep reading the first paragraph then you will get
to the bit where interfaces are useful, I could give you a textbook example,
but here is one example that is useful in everyday C# life. With C# you have
garbage collection which is non deterministic, meaning once your objects go
out of scope you cannot say exactly when their underlying resources are going
to be cleaned up, it is up to the garbage collector to decide when this will
happen. This is normally not an issue but when your objects have references
to unmanaged resources such as network connections / database connections /
unmanaged memory allocations etc you want to be able to control that these
resources are cleaned up as soon as you are finished using the object, since
the GC is not aware that these resources are limited and will not know it
should clean up the object ASAP.

..Net has an interface called IDisposable which has a single method "void
Dispose()". The idea being that when this method is called all internal
state is cleaned up, so you can control exactly when network connections / db
conn etc are closed. Now what it means for two different objects to "clean
up" their internal state can be completely different, these objects do not
share any common cleanup code so it makes no sence to make them inherit from
some base class just so they both have the Dispose method, objects that
implement IDisposable can define how objects can be cleaned up inside the
Dispose method, so in code you end up with:

void MyFunction()
{
MyDatabaseObjec t db = null;
MyCustomMemoryC reator mc = null;

try
{
db = new MyDatabaseObjec t();
mc = new MyCustomMemoryA llocator();

//do some stuff
}
finally
{
//going out of scope, lets release all unmanaged state
//this is inside the finally since we want to make sure these resources
//are cleaned up even if there is an exception in the function.
if(db != null) db.Dispose();
if(mc != null) mc.Dispose();
}
}

It's kind of a pain to have to use the try / finally each time so you can
use the "using" keyword which takes an object that implements the IDisposable
interface, the objects passed to this language construct may not be related
at all, all this construct knows is that on each object it can call a method
called Dispose() once the current scope of the using statement is exited.
//The following code is semantically equivalent to the above code:
using(MyDatabas eObject db = new MyDatabaseObjec t())
using(MyCustomM emoryCreator mc = new MyCustomMemoryC reator())
{
//do some work

//Dispose method on each object will automatically get called.
}

So you can see how even though you have objects that need to implement the
same interface i.e. a Dispose method, they may have nothing in common so you
do not want those objects to inherit from a base class since they share NO
common code just a common interface.

Kind of long winded but a good example of why interfaces are useful :-)

Mark.
--
http://www.markdawson.org
"jm" wrote:
I am having trouble understanding the purposes of an interface, even
though the concept of interfaces is around me all the time (user
interface, for example). I'm just not understanding software
interfaces.

Like anything else, it appears Interfaces are by design something that
requires documentation. I know this may be obvious, but if I have a
class A and I say, well, class A implements IMyInterface. The fact
that it implements IMyInterface is supposed to mean something to me. I
am guessing that MS and other Sun have a lot of well defined and
documented interfaces that I would eventually care about.

I guess I am looking for a time when their absence would require them.
I think that by seeing their missing from a given situation that
requires them (or would be hard without them), I might see and
understand their necessity and understand.

Does anyone have an example of this? I hope this made some sense.

Thank you.

Nov 4 '06 #5
Lets say you have a bunch of ducks. You have your mallard, rubber
duckie, etc. Now all these ducks to the same things, but they do them
in different ways. Ducks quack, but mallard's quack is "quack!" and the
rubber duckie's quack is "squeak!" They both swim, but slightly
differently. More over, however, they're all ducks.

You want to implement ducks but C# is a strongly typed language. You
can't write code to handle a mallard and a rubber duckie because
they're different classes - unless you use interfaces.

So you define a "Iduck" interface and list the behaviors (methods) that
all ducks have: swim, quack, fly, etc. Then, when you define each
individual duck class, implementing the Iduck interface, with different
implementations for swim, fly, etc. and then you can write code that
handles "Iducks" and you can give it any old kind of duck - because
they're all ducks (i.e. implementing the same interface) with the same
general duck behaviors (methods).

Note that when a class implements an interface it is somewhat like
inheritance. Please don't anyone start arguing the technical nits here;
as a practical matter you can think in terms of "a mallard is an Iduck"
and that works fine. The neat thing is that a class can implement any
number of interfaces, while it can only inherit from one class. It's
not unusual to see interfaces with no methods defined; it's only
purpose being to handle anything as that general "type".

On 2006-11-03 16:56:17 -0600, "jm" <ne***********@ gmail.comsaid:
I am having trouble understanding the purposes of an interface, even
though the concept of interfaces is around me all the time (user
interface, for example). I'm just not understanding software
interfaces.

Like anything else, it appears Interfaces are by design something that
requires documentation. I know this may be obvious, but if I have a
class A and I say, well, class A implements IMyInterface. The fact
that it implements IMyInterface is supposed to mean something to me. I
am guessing that MS and other Sun have a lot of well defined and
documented interfaces that I would eventually care about.

I guess I am looking for a time when their absence would require them.
I think that by seeing their missing from a given situation that
requires them (or would be hard without them), I might see and
understand their necessity and understand.

Does anyone have an example of this? I hope this made some sense.

Thank you.

Nov 4 '06 #6
Interfaces can be used for lots of things:
i. Form a binding contract that a particular class supports a
particular feature.
ii. Can be used for subsystem isolation, especially handy if your
software relies on 3rd party products.
iii. Method filtering. If you have a class with 1000's of methods,
you can use several interfaces with that class so that you can view that
class as it's interface implementation. This has the advantage that
the intellisense only picks up the relevant methods.
iv. Polymorphism (vs Class hierarchy Polymorphism).
v. Some technologies like COM use interfaces exclusively and use it
to black box the internal classes.
vi. Similar to ii above in that they can be used to develop a
Plug'n'Play system, or if you don't want to go that far, they can be used
with class factories to give you maximum flexibility with regard to
switching in and out various subsystems of your application.

There are many more! Interfaces, once you learn them become indispensable.

If you want me to elaborate on any of the above, I'd be happy to. I
personally find them very handy.
--
RobP
'There are only 10 types of people in this world - Those that understand
binary and those that don't'
"jm" <ne***********@ gmail.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
>I am having trouble understanding the purposes of an interface, even
though the concept of interfaces is around me all the time (user
interface, for example). I'm just not understanding software
interfaces.

Like anything else, it appears Interfaces are by design something that
requires documentation. I know this may be obvious, but if I have a
class A and I say, well, class A implements IMyInterface. The fact
that it implements IMyInterface is supposed to mean something to me. I
am guessing that MS and other Sun have a lot of well defined and
documented interfaces that I would eventually care about.

I guess I am looking for a time when their absence would require them.
I think that by seeing their missing from a given situation that
requires them (or would be hard without them), I might see and
understand their necessity and understand.

Does anyone have an example of this? I hope this made some sense.

Thank you.

Nov 4 '06 #7
Here's a (close to) real world example:

Your manager gives you the following task: "I need a combobox that is
synchronized with a TabControl such that when an item is added to the
combobox, a tab is added to the tabcontrol, when an item is deleted from the
combobox, the matching tab is deleted from the tabcontrol and when an item
is selected in the combobox, the matching tab is selected in the tabcontrol.
I need it yesterday."

Now you can solve this problem by deriving a new control from ComboBox and
add code to manipulate a TabControl directly (for example, you add a
TabControl property to your ComboBox, override the Add, Delete and Change
methods and add, remove, change tabs if the property is assigned) and you
would be finished quite soon (although not yesterday). This is inheritance
and all is good.

The next day your manager comes in to you and say "Ok, that looks nice, but
we really need to do something about that TabControl, it looks boring. I've
found this really cool, whiz-bang TabControl that I've payed good $$$'s for
and the best thing is that it isn't derived from the MS TabControl, so it is
a lot better by default" (this is his way to try to be "in" with the
coders - he once attended a manager meeting where he was told that all
coders hate MS). You sigh and start to implement a new ComboBox that can
connect to the whiz-bang TabControl (you are keeping the other one around
just in case - you are a seasoned developer and not stupid).

You now have two different ComboBoxes that does the same thing with two
different TabControls. You feel that all is not so good anymore and you hope
that your manager doesn't come into your room tomorrow with another
whiz-bang control (like a really cool ComboBox that isn' t derived from the
MS ComboBox) or even a whole new set of controls he wants to synchronize.
You are probably not so lucky and you wish there was some other way to solve
this problem. There is. Use interfaces.

So how do you solve this problem using interfaces? Well, you start by
defining the behavior that is required (Add, Delete, Change methods comes to
mind) and you put them in an interface. Let's call it IControlSync:

public interface IControlSync
{
public int Add(string Value);
public void Delete(int Index);
public void Change(int Index);
}

You now proceed to derive a new control from ComboBox and add an
IControlSync property to it. Whenever Add, Delete and Change is called in
your ComboBox, you call the appropriate IControlSync interface method (if it
has been assigned, of course). You don't know what type of control you are
calling (or if it is a control at all), you are just calling the
IControlSync methods. That is all you know and that is all you need to know.
You obviously also derive a new control from TabControl that *implements*
the IControlSync methods. You must also assign this new control to the
IControlSync property of the ComboBox (I assume you knew that already).

Now, when the boss comes in the second day (remember?) and wants to use his
new TabControl, you just smile smuggly, install the whiz-banger, derive a
new TabControl from it and quickly implement the IControlSync methods. When
done, you just assign the new TabControl to the ComboBox IControlSync
property to make them synch.

Now imagine the possibilities if the ComboBox also implements IControlSync
and the TabControl had an IControlSync property. You would have several
different controls that can be synchronized with each other in any which way
you like by just assigning to the appropriate IControlSync property.

Whenever a control needs the same functionality, you just derive a new
control and implement the IControlSync methods on it. As a bonus, since
IControlSync has nothing to do with the class it is implemented on, the
implementor can be *any* class, not just a Control (the name was chosen to
confuse noobs).

Hope that "spins" it for you.

--
Regards, Peter
Nov 4 '06 #8
All excellent examples. I would like to piggyback the OP and ask about
real world examples of RETURNING an interface. I have seen code that
does as such:

public ISomething Somemethod()
{
....do work
return SomeObject;
}

or some such. What I read from this is that the calling code must
assign the return value to any object which has an ISomething
interface? Such as:

ISomething varsGettingHard ToDescribe = Somemethod();

But the returning object loses it's inherent SomeObject class methods.
Ive seen (something like) this before, and have trouble at times
following whats going on, what is gained, and what is lost.

Nov 5 '06 #9

jm wrote:
I am having trouble understanding the purposes of an interface, even
though the concept of interfaces is around me all the time (user
interface, for example). I'm just not understanding software
interfaces.

Like anything else, it appears Interfaces are by design something that
requires documentation. I know this may be obvious, but if I have a
class A and I say, well, class A implements IMyInterface. The fact
that it implements IMyInterface is supposed to mean something to me. I
am guessing that MS and other Sun have a lot of well defined and
documented interfaces that I would eventually care about.

I guess I am looking for a time when their absence would require them.
I think that by seeing their missing from a given situation that
requires them (or would be hard without them), I might see and
understand their necessity and understand.

Does anyone have an example of this? I hope this made some sense.

Thank you.
This question comes up with some regularity in this newsgroup. Try
searching for "interface" in the archives. Here is one thread I found
from a while back:

http://groups.google.com/group/micro...487039d5505a47

Nov 5 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
6319
by: MLH | last post by:
I would like to test some of this code in the debug window... Option Compare Database Option Explicit Private Sub Command0_Click() #If Win32 Then MsgBox "Hey! It's WIN32." #End If End Sub
22
2109
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to a class, that when implemented, they require the implementing calss to make sure that each of the properties, functions etc. are handled by the class. (????) What I am really having a problem with is how they overcome the limitation
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9537
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9243
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.