Whats an interface?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Im new in this of the .net, and more or less i get the concepts of classes
events, delegates, but still i dont understand in wich kind of programs the
interfaces are very useful...

I would like to have a clear vision about the use of this and in wich cases
are better the use.

Could you put some examples (no neccesary code, only examples as ideas)
about when the use of interface its the best way to follow?

Any help would be grateful...
Thanks.
 
If you want to create a UserControl that show an array of object you don't
know wath property of the object you have to show... for example...

You create the IMyUserControl in wich you create the ShowObj() method.

Every class you want could be showed in your UC must implements the
IMyUserContol interface.

In the UserControl, when you have to show the object, you use the interface:

((IMyUserControl)theObjectToShow).ShowObj()

ta... da...

Sorry for my english...
 
For example, you could write a sorting algorithm that uses interfaces (e.g.
IList and IComparable) to access the data to be sorted: That way the same
code could be used to sort an array, an ArrayList or some linked list class,
as long as each of them implement the same interface.

You can do something similar with abstract base classes; The advantage of
interfaces is that a class can have only one direct base class, but can
implement any number of interfaces.

Interfaces are also quite commong for "loose-coupling" parts of big
applications: Each application part only exposes a set of interfaces, the
internals (like what each class implements, what it's derived from) stay
hidden;

Niki
 
Hi Josema,

Let's understand the concept behind interfaces with an example. :)

We know of several birds that can fly...like sparrows, doves, crows etc. We
also know of several other entities that can fly...like kites, planes,
shuttles, rockets etc. But the way a sparrow flies is completely different
from the way a plane does. The way a plane flies is also completely different
from the way a shuttle does...We are sure of one thing but...all these
entities can FLY.
In terms of programming, we would define an interface IFlyingObjects with
the method Fly. And then would create seperate classes for each of the
examples above, each of which would implement the interface IFlyingObjects.
This would ensure that each of the object would have defined a method Fly
with their own specific implementation.

Interfaces are something the qualifies an entity. For example, calling a
person an actor would automatically mean that he/she is capable of *Acting*.
Calling another person a painter would mean he/she is capable of *Painting*.
But these people are all *People* as well - only the different qualities vary.

As per the programmatic definition, Interfaces define a contract - which
means that if a class implements an interface, we can be sure that it defines
all the members declared in the interface.

HTH,
- Rakesh
 
Josema,

I usually think of interfaces as something to consider when I have a
particular signature of methods and/or properties that need to be consistent
across two or more classes that are not in the same inheritance tree; or
where it represents a bit of functionality that you want to be optional for
specific concrete classes.

A good example of the first situation is IDisposable. Any random class can
implement IDisposable if it needs to be responsible for unmanaged resources.
This is functionality that has wide applicability and describes behaviors
that are fairly generic.

One of the handy uses of interfaces is that you can cast to them like you
can a "real" type. So this enables code such as the following, which might
be used when iterating through a collection of objects, some of which are
disposable:

if (someObject is IDisposable) {
((IDisposable)someObject).Dispose();
}

Without the interface cast you would have some huge switch statement that
would have to cover every possible disposable type that might be
encountered, which would be error-prone, tedious and fragile.

--Bob
 
Back
Top