473,401 Members | 2,125 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,401 software developers and data experts.

Interfaces - Why?

Ant
Hi,
I’m wondering what practical use is there for creating interfaces. I can
fully appreciate creating an abstract class for the purpose of inheriting,
but why an interface? Is it just to promote polymorphism while keeping
implementation unique? I dunno; I know I must be missing something big here.
Any clues would, as always, be greatly appreciated.
Thank you
Ant

Nov 17 '05 #1
7 1564
Ant wrote:
I'm wondering what practical use is there for creating interfaces.
I can fully appreciate creating an abstract class for the purpose
of inheriting, but why an interface?


The big practical reason is that interfaces are the closest C# comes to
multiple inheritance. You can only inherit from one implementation, but you
can implement multiple interfaces.

Semantically and design wise, this makes a lot of sense to me. While the
base class continues to represent a data entity, such as a customer, order
or inventory item -- a nominative noun -- the multiple interfaces the class
can implement represent things that can be *done with it*, the instrumental
case.. For example, the IFormattable interface provides means to transform
any object to a human-readable string representation. This is an action
that is not, by itself, specific to any particular type of data (but is
specific as to the desired result).
--
Chris Priede (pr****@panix.com)
Nov 17 '05 #2
The confusion is common as you really have to think about the problem at
hand. In fact, on occasion, I have started with an interface and decided
that a base classes was better, or vise versa.

It really is a matter of what you want from your design. Here are some
questions that you might ask.

1) Does your class already inherit from some class? If this is the case,
anything else must be through an interface.
2) Does any common behavior exist between the derived classes or share
data? If this is the case, you might consider a class.
3) Do you want to simplify your derived classes, in this case maybe a
consider a base class. Just like the previous item, you might be able to
put must of your code in the base class and have very little work done by
the derived classes. With interfaces, all the code has to be implemented by
the classes implementing the interface.
4) If there a relationship between the classes? If there is no
relationship, then an interface is appropriate, otherwise a base class.

Interfaces should be kept very small, just a few members, and they should do
one thing. Take a look at some of the interfaces provided by the framework
and you will see that they have few members.
"Ant" <An*@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
Hi,
I'm wondering what practical use is there for creating interfaces. I can
fully appreciate creating an abstract class for the purpose of inheriting,
but why an interface? Is it just to promote polymorphism while keeping
implementation unique? I dunno; I know I must be missing something big
here.
Any clues would, as always, be greatly appreciated.
Thank you
Ant

Nov 17 '05 #3
Ant
Would it be thought of as wrong if one was to put all the properties in a
base class & then seperate methods into groups & put them in various
Interfaces or is this irrelevent?

Thanks for your prior answers as well

ant

"Peter Rilling" wrote:
The confusion is common as you really have to think about the problem at
hand. In fact, on occasion, I have started with an interface and decided
that a base classes was better, or vise versa.

It really is a matter of what you want from your design. Here are some
questions that you might ask.

1) Does your class already inherit from some class? If this is the case,
anything else must be through an interface.
2) Does any common behavior exist between the derived classes or share
data? If this is the case, you might consider a class.
3) Do you want to simplify your derived classes, in this case maybe a
consider a base class. Just like the previous item, you might be able to
put must of your code in the base class and have very little work done by
the derived classes. With interfaces, all the code has to be implemented by
the classes implementing the interface.
4) If there a relationship between the classes? If there is no
relationship, then an interface is appropriate, otherwise a base class.

Interfaces should be kept very small, just a few members, and they should do
one thing. Take a look at some of the interfaces provided by the framework
and you will see that they have few members.
"Ant" <An*@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
Hi,
I'm wondering what practical use is there for creating interfaces. I can
fully appreciate creating an abstract class for the purpose of inheriting,
but why an interface? Is it just to promote polymorphism while keeping
implementation unique? I dunno; I know I must be missing something big
here.
Any clues would, as always, be greatly appreciated.
Thank you
Ant


Nov 17 '05 #4
"Ant" <An*@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
Hi,
I'm wondering what practical use is there for creating interfaces. I can
fully appreciate creating an abstract class for the purpose of inheriting,
but why an interface?


The nature of Object Oriented Programming is this - take an interface, take
some behaviour, vary that behaviour independent of that interface.

There are lots of reasons to use interfaces, and sometimes reasons to not
use them. When developing an application, it is generally considered a trait
of an object oriented design for the client to own the interface to some
code. For example, in the good ol' procedural days, you'd have high level
policy modules depending on low level policy modules. In the OO world, those
dependencies are inverted. Low level policy modules should depend on high
level policy modules. i.e. I'm a high level module, I need some low level
policy implemented. I publish an interface for this low level code. The low
level code implements this interface and provides the functionality.

To do this without interfaces means exposing a base implementation for low
level modules to inherit, and that's usually far too restrictive a way to
develop code.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #5
Ant wrote:
Hi,
I’m wondering what practical use is there for creating interfaces. I can
fully appreciate creating an abstract class for the purpose of inheriting,
but why an interface? Is it just to promote polymorphism while keeping
implementation unique? I dunno; I know I must be missing something big here.
Any clues would, as always, be greatly appreciated.
Thank you
Ant

Ant wrote: Hi,
I’m wondering what practical use is there for creating interfaces. I can
fully appreciate creating an abstract class for the purpose of inheriting, but why an interface? Is it just to promote polymorphism while keeping
implementation unique? I dunno; I know I must be missing something big here. Any clues would, as always, be greatly appreciated.
Thank you
Ant


The purpose of an interface is to specify for a class that it "adheres
to a contract", or put simply, it implements a specific set of
functionality.

Let me describe an example.

Let's say you have a lot of visual controls that you use in your
WinForms applications. These inherit from normal .NET runtime visual
controls, and thus, by necessity, inherit from a lot of different classes.

If you now wanted to add a specific function to all of your own visual
controls so that you could loop through all the controls on a form and
say something like this:

if (IsMyControl(control))
control.DoSomethingSpecificForMyControls();

then you would have to do the following:

1. Implement DoSomethingSpecificForMyControls in all of your controls
(of course)
2. The above if-statement would have to be written like this:

if (control is MyButtonType)
((MyButtonType)control).DoSomethingSpecificForMyCo ntrols();
elseif (control is MyTextBoxType)
((MyTextBoxType)control).DoSomethingSpecificForMyC ontrols();
elseif ...

and so on. Since you can't change the base class (because it's in the
runtime and thus out of your control, and it's also a lot of different
base classes in this case), you can't really do anything but lift the
method up to the first point where you have access, in your own classes.

The problem of using these controls now becomes twofold:

1. You can't easily detect wether a class is one of your own (or rather,
that it implements that specific function you need) without listing up
all the class types you know of, or using RTTI and seeing if that
specific method name is present.
2. You can't easily call that method without either casting the object
to a specific class (which differs for each) or by going through RTTI to
invoke it dynamically.

Listing all the classes is certainly doable, but the problem becomes
maintenance. The second you add a new visual control you need to go back
and find all those lists and add the new one with the appropriate code.

Looking for the method name is a kludge, but again doable, if the method
name is reasonably obscure such that you can assume nobody else would
ever want to add such a method. If your method name is "Load" or
similar, you might have problems.

So, enter interfaces. An interface is a way to do the following:

1. Tag a class as "this class supports functionality XYZ"
2. Easily typecast the class to a specific interface so that you can
call the method(s)

In other words, the above if-statement becomes:

if (control is IMyControl)
((IMyControl)control).DoSomethingSpecificForMyCont rols();

That's it. Any new classes you write only need to implement that
interface and all existing code that already knows how to deal with it
will be able to handle it.

Think of your classes as a cube. When you deal with the class directly
you are looking at one side of the cube. When you need to deal with the
class in a different way, you turn the cube to find a side with the
buttons you need to use. As long as you know how to use a specific side
of the cube that contains the functions you know about, you don't need
to care about what the other sides of the cube contains.

This is also the reason why a lot of things in libraries are best left
to interfaces, so that method parameters use the interface types instead
of a specific class type, locking you into inheriting from a common base
class. If the method in the runtime uses an interface, you can implement
that interface in your own classes (regardless of what they inherit
from) and pass objects of that type to that method, and it'll work.

I hope this become clearer now, otherwise just let me know what is still
unclear and I or someone else will probably clear it up.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #6
Hello Ant,

Over there is an cognitive article of abstract classes versus interfaces
http://www.codeproject.com/useritems...Interfaces.asp
hope it helps u
A> I'm wondering what practical use is there for creating interfaces. I
A> can
A> fully appreciate creating an abstract class for the purpose of
A> inheriting,
A> but why an interface? Is it just to promote polymorphism while
A> keeping
A> implementation unique? I dunno; I know I must be missing something
A> big here.

---
WBR,
Michael Nemtsev
"At times one remains faithful to a cause only because its opponents do not
cease to be insipid. (c) Friedrich Nietzsche"
Nov 17 '05 #7
Ant,

I generally consider interfaces when there is behavior that is
applicable independent of a class hierarchy. The interfaces implemented
in the .NET framework are good examples. IDisposable for example could
apply to any class, regardless of inheritance, that needs to deal with
unmanaged resources. This isn't the only possible use for interfaces,
but I find that in practice, it accounts for most of them.

Another way to look at interfaces is as a workaround for some of the
bondage and discipline of strongly typed, early bound environments.
Frankly interfaces are not that necessary in a late bound system. For
example, I confess to occasionally missing FoxPro's ability to not care
so much what a particular runtime instance inherits from so much as
whether a method you're calling against it actually exists in that
instance. There are just times when you need a method that can take any
object that knows how to (say) PerformFubar(). If you create an IFubar
interface that implements that method, then you can cast to the IFubar
type when you need to pass around objects where the methods that will
receive the instance care more about what they need to *do* than what
the objects *are*. Since every argument you pass has to be a specific
type, this allows you to satisfy that requirement while still having the
flexibility to pass in a variety of objects that all share a particular
capability.

If a particular functionality is not sufficiently generic to transcend a
class hierarchy then abstract classes / members are the way to go,
particularly since you are likely to need to inherit / specialize
behaviors in that scenario.

--Bob

Ant wrote:
Hi,
I’m wondering what practical use is there for creating interfaces. I can
fully appreciate creating an abstract class for the purpose of inheriting,
but why an interface? Is it just to promote polymorphism while keeping
implementation unique? I dunno; I know I must be missing something big here.
Any clues would, as always, be greatly appreciated.
Thank you
Ant

Nov 17 '05 #8

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

Similar topics

1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
8
by: John | last post by:
What is the purpose / benefit of using an interface statement? It doesn't seem like anything more than a different way to make a class... (except you can't define any procedures in an interface...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
22
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...
18
by: Tony | last post by:
class Interface { public: virtual void DoItNow()=0; }; class A: public Interface { public: void DoItNow(); // satisfies interface explicitly
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
10
by: hyperboreean | last post by:
Hi, Probably it has been asked before, but I'll still ask. Why doesn't python provide interfaces trough its standard library? Or it was ever proposed to be included in the language? Zope's...
23
by: A.Gallus | last post by:
If I declare a function pure virtual: class A { virtual void myfunc() = 0; } and I derive a class from A: class B : public 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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...

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.