473,763 Members | 9,161 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
27 3860
jm wrote:
>
Do interfaces get used in ASP.NET or is that more of an executable world?
Well, I don't write for ASP.NET, but yes, they would be used there,
too.

Rather than talking about ducks, I'd rather point out a classic use of
interfaces: your business layer needs to talk to a data layer in order
to persist / fetch business information. Typically what one does here
is write a bunch of Data classes that mediate between the business
layer objects and their representation in a SQL database. No problem so
far.

Now say that I want to demo my product on the road, and I don't
necessarily have access to a SQL database. I could, of course, use a
lightweight SQL database product on my laptop, but another option is to
write a whole other data layer that doesn't use a relational database
on the back end. Perhaps I want to store some demo data in XML files
and read from them instead. How would I do that?

Well, my SQL database persistence classes have their own inheritance
hierarchy: CustomerSqlData , say, inherits from SqlDataHandler, which
has lots of functionality common to classes that need to talk to SQL
databases. If I inherit CustomerXmlData from SqlDataHandler, or from
CustomerSqlData , then I get a bunch of code that has to do with SQL
databases that I don't want. If I create a whole second inheritance
hierarchy--CustomerXmlData inherits from XmlDataHandler--then how do I
tell my business layer that CustomerSqlData and CustomerXmlData are
really the same thing, just implemented in two different ways?

Interfaces are the answer. I create an interface called ICustomerData,
and simply tell my business layer class that it is talking to an
ICustomerData object. It doesn't need to know which one. The objects
don't need to be related by inheritance. In effect, my business layer
object is specifying a _contract_: "I will deal with any object that
implements the following functionality.. ." rather than specifying a
particular _class_ with which it will work. This frees you to implement
the required functionality in any class, anywhere in the inheritance
hierarchy, and effectively you can "plug and play" your data layer into
your business layer. You can even plug in some data classes that read
from XML and some that read from SQL if that turns out to be useful.

Interfaces, then, give you the capability to "chunk off" parts of your
application and build plug-and-play parts to implement the various
chunks. Each layer of your application knows only that the object it's
talking to implement the required contract (interfaces).

This sort of layering and plug-and-play capability applies equally well
to ASP.NET and WinForms.

Nov 7 '06 #21

jm wrote:
This may be dumb, but say I had some method that took the parameter:

public void RunAppliance (IOutlet electricalAppli ance)
{
electicalApplia nce.TurnOn();
}

As long as the electicalApplia nce class reference sent to the method
RunAppliance implements the IOutlet interface then objects like::

refrigerator
toaster
tvset

all can/must use the method TurnOn() in there respective classes. Now
if I knew every single electrical appliance that would ever be designed
had a TurnOn() method, then I may not need the Interface, but if I did
not have the Inteface, then I would have to do something like you did
above with (object electricalAppli ance) and do all sorts of tests on
every single possible appliance.

Because I have an Interface I, a) know the contract and that anything
that implements the IOutlet interface must have a TurnOn() method, b)
because of this I can created a reference like (IOutlet
electricalAppli ance) and be sure at compile time all reference are
"clean."

The advantage, I'm sure there are others, is in the ability to not have
know every single thing about a given class except that it implements
IOutlet and therefore all these otherwise unrelated classes now have
something in common. They implement the same interface.

This also helps me kind of see why there are muliple interfaces that
can be implemented. I can see why some or all appliances might turn
on, but not all heat up. A stove and toaster might have a IHeatUp
inteface, but a refrigator probably won't. We need these unique but
properties and methods for the uninherited classes that are not related
in a hierachy, but they are related in a implementation. They all do
something called the same thing, just differently.

I think I'm still missing some of it, but I think I have a better
understanding of it. Sorry if I repeated myself too much and being
long winded. It was more for me than anything. Hopefully some of it
is even right!
By Jove, he's got it!

Yes, that's exactly it. Looking at it the other way around, interfaces
allow you to design your class hierarchy according to what things
really are (is-a relationships) rather than artificial concerns about
shared functionality. In your example, it doesn't require you to lump
the Light class in with the ToasterOven class just because they both
use electricity and both need to be turned on. It allows you to put the
WoodBurningStov e under the Oven class along with the ToasterOven
without worrying about how it's going to implement TurnOn().

Of course, these things often aren't clear from the outset of class
design. What initially looks like a perfectly reasonable assumption
(all appliances can be TurnedOn) may later turn out to be false, at
which point one typically extracts a method into an interface and has
all existing classes implement the interface before adding into the
hierarchy the new class that's the exception to the rule. It's called
refactoring, and it goes on all the time.

Nonetheless, whether at the beginning of the design process or later
due to refactoring, interfaces are indispensible in single-inheritance
languages such as C# and Java. A multi-interitance language such as C++
accomplishes this in a different way.

Nov 7 '06 #22
On 7 Nov 2006 07:21:16 -0800, "jm" <ne***********@ gmail.comwrote:
>Thank you.

Why wouldn't I have an abstract class and override the abstract method,
since all the obects inherit from the same top level object (ducks)?
Bob Jones wrote:
>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
implementation s 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.
jm,

Thank you for asking this question.

The rest of you that helped answer the question and especially Bruce Wood for
one of the best and most understandable explanations of interfaces I've read. It
lit up some ILightBulb Interfaces in this old brain.
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Nov 8 '06 #23
Bruce Wood wrote:
Take the duck example that was given by another poster. Do you really
want a RubberDuck inheriting from Duck? What if Duck inherits from
FlyingBird which inherits from Bird which inherits from Mammal?
So now everybody understands interfaces, but we're all wondering why
Bird inherits from Mammal :-)
Nov 8 '06 #24
jm

Bruce Wood wrote:
jm wrote:

Do interfaces get used in ASP.NET or is that more of an executable world?

Well, I don't write for ASP.NET, but yes, they would be used there,
too.

Rather than talking about ducks, I'd rather point out a classic use of
interfaces: your business layer needs to talk to a data layer in order
to persist / fetch business information. Typically what one does here
is write a bunch of Data classes that mediate between the business
layer objects and their representation in a SQL database. No problem so
far.

Now say that I want to demo my product on the road, and I don't
necessarily have access to a SQL database. I could, of course, use a
lightweight SQL database product on my laptop, but another option is to
write a whole other data layer that doesn't use a relational database
on the back end. Perhaps I want to store some demo data in XML files
and read from them instead. How would I do that?

Well, my SQL database persistence classes have their own inheritance
hierarchy: CustomerSqlData , say, inherits from SqlDataHandler, which
has lots of functionality common to classes that need to talk to SQL
databases. If I inherit CustomerXmlData from SqlDataHandler, or from
CustomerSqlData , then I get a bunch of code that has to do with SQL
databases that I don't want. If I create a whole second inheritance
hierarchy--CustomerXmlData inherits from XmlDataHandler--then how do I
tell my business layer that CustomerSqlData and CustomerXmlData are
really the same thing, just implemented in two different ways?

Interfaces are the answer. I create an interface called ICustomerData,
and simply tell my business layer class that it is talking to an
ICustomerData object. It doesn't need to know which one. The objects
don't need to be related by inheritance. In effect, my business layer
object is specifying a _contract_: "I will deal with any object that
implements the following functionality.. ." rather than specifying a
particular _class_ with which it will work. This frees you to implement
the required functionality in any class, anywhere in the inheritance
hierarchy, and effectively you can "plug and play" your data layer into
your business layer. You can even plug in some data classes that read
from XML and some that read from SQL if that turns out to be useful.

Interfaces, then, give you the capability to "chunk off" parts of your
application and build plug-and-play parts to implement the various
chunks. Each layer of your application knows only that the object it's
talking to implement the required contract (interfaces).

This sort of layering and plug-and-play capability applies equally well
to ASP.NET and WinForms.
"The objects
don't need to be related by inheritance. In effect, my business layer
object is specifying a _contract_: "I will deal with any object that
implements the following functionality.. ."

Very helpful. Thank you again.

One of the things that was baffling me was constanting thinking about
inheritance. All of a sudden interfaces broke that "rule" that was in
my mind. If I undersand correctly, I can now think of related
behaviors and properties that objects have and not strictly "is-a"
relationships and inheritance.

But one thing that doesn't make total sense to me is that if I have
ClassA and ClassB is derived from ClassA, then I can do things like:

ClassB b = new ClassB();
ClassA a = b; //I thought I could do this anyway

because ClassB is a ClassA.

With interfaces, I can now assign objects that implement the same
interface to a reference type of that interface. That throws me
because the objects are not really related except by behavioral
aspects. If ClassA and ClassB are not "kin," but both implement
IMyInterface, then I can assign them to a reference of type
IMyInterface. I know it is allowed, but it seems the total opposite of
all the other "is-a" class stuff I already had neatly tucked away with
the whole inheritance thing. On the other hand, if I go to use these
items that implement the same interface, what am I saying other than I
want to use the methods they all have in common, which does make sense.
Perhaps it is two sides of the coin of inheritance.

Don't worry if you don't understand what I'm saying. I'm more rambling
than anything It basically says, I think, that it kind of blows up the
inheritance model to assign unrelated (by inheritance) items to the
same reference type (yet they are sort of related by behavior)..

Nov 8 '06 #25
Bruce Wood wrote:
languages such as C# and Java. A multi-interitance language such as C++
accomplishes this in a different way.

Although, of course, users of those multiple-inheritance languages often
choose to so it the same way, because it's so often the Right Thing to
do -- it's not uncommon to have C++ classes with no members and all
methods abstract, which is essentially the C++ way to have an interface.
Nov 8 '06 #26
jm

Tim Rowe wrote:
Bruce Wood wrote:
languages such as C# and Java. A multi-interitance language such as C++
accomplishes this in a different way.


Although, of course, users of those multiple-inheritance languages often
choose to so it the same way, because it's so often the Right Thing to
do -- it's not uncommon to have C++ classes with no members and all
methods abstract, which is essentially the C++ way to have an interface.
Now my problem is that I want to write a bunch of interfaces and don't
have a way to apply it. I wanted to play with my new toys. I
mentioned asp.net in another thread, and I know this is naive, but I've
been so brainwashed that I can't even think of a whole lot to do with
an executable on the desktop anymore, yet my computer is chock full of
them. Games, embedded systems, this browser. Big stuff. I'm afraid
I'll never get to use what I learned. I'll be looking everywhere for
interfaces now..at least until next chapter which is on Generics. Then
i'll try an find that.

Nov 8 '06 #27

Tim Rowe wrote:
Bruce Wood wrote:
Take the duck example that was given by another poster. Do you really
want a RubberDuck inheriting from Duck? What if Duck inherits from
FlyingBird which inherits from Bird which inherits from Mammal?

So now everybody understands interfaces, but we're all wondering why
Bird inherits from Mammal :-)
Ummm... it's all of those furry, warm-blooded birds flying about....
(He says, trying desperately to save face....) :-)

Nov 8 '06 #28

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

Similar topics

22
6322
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
2113
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
9387
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10002
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8822
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7368
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
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
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...
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2794
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.