Z
Zytan
Aren't all classes interfaces?
What constitutes an interface (and with it, the "I" prefix
distinction)?
Zytan
What constitutes an interface (and with it, the "I" prefix
distinction)?
Zytan
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Zytan said:Aren't all classes interfaces?
What constitutes an interface (and with it, the "I" prefix
distinction)?
All classes define (at least) one interface. But they are not the same.
An interface is a contract. It means that any class that implement it has to
implement all the members declared in it.
Take a look at MSDN for a better more in depht explanation
An interface is a class (or struct). It contains methods, properties,Aren't all classes interfaces?
What constitutes an interface (and with it, the "I" prefix
distinction)?
Zytan
indexers or events. It does not provide implementations for any of
its methods.
The advantage of an interface is that any other class (or interface)
can "inherit" (or implement) that interface. It is the C# way to get
something similar to multiple inheritance in C++. Any class that
implements an interface must supply all the members specified in the
interface.
Thanks, rossum, this explains a lot.
But, I need to be more experience before I can understand why an
interface is needed. I never used multiple inheritence before.
rossum said:An interface is a class (or struct). It contains methods, properties,
indexers or events. It does not provide implementations for any of
its methods.
Zytan said:But, I need to be more experience before I can understand why an
interface is needed. I never used multiple inheritence before.
There's much more to interfaces than providing a workaround for the
lack of multiple inheritance. It allows you to use a class knowing only
that it implements the interface.
It's a way of providing polymorphism which is independent of the class
hierarchy (i.e. there's no particular base class needed).
Chris Nahr said:Well yes, but the same is true if you use a class knowing only that it
implements a given base class...
True.
That's only true because .NET doesn't support multiple inheritance in
the first place. Otherwise you could attach some new base class at an
arbitrary point in any class hierarchy, in the same way as you can now
attach an arbitrary interface. You would no longer consider that
particular base class part of a "hierarchy" either.
I think it's misleading to say that an interface is a class or a
struct. An interface is a distinct kind of type in itself. It may be
*implemented* by a class or a struct, but in itself, it is neither.
lack of multiple inheritance. It allows you to use a class knowing only
that it implements the interface. Probably the most commonly used
interface in .NET is IDisposable, which defines the Dispose method.
That means that I could have a method:
void DisposeOfSomething (IDisposable x)
{
x.Dispose();
}
and *anything* implementing IDisposable could be passed to the method.
It's a way of providing polymorphism which is independent of the class
hierarchy (i.e. there's no particular base class needed).
Thanks, these help a lot.
You could have implementations that are totally different, say, SQL
data, or flat file (text file) data, or XMl data, and have an
interface that is the same for all of them. And the implementations
of these would use the same interface. The interface *forces* that
all of it is implemented (very good c#) which forces that it is done
correctly. Impressive concept.
Zytan
I understand it now. A C# interface (the keyword interface) which
should have the "I" prefix is not the interface portion of a class.
They are both interfaces just as pointers and references are both
references (in plain english). But I understand the distinction now.
IClass means that it is an 'interface' not a 'class'.
Zytan
Zytan said:I have no experience with polymorphism so this is over my head, but I
think I understand. I want two totally different implementations that
show the same interface to the caller. This way, the called need not
care about the internal code, it just uses it, much like, say,
electric motors vs gas motors work under the hood, but have the same
steering wheel and 'gas' pedal to make it move. So, this concept,
which I understand, is basically what polymorphism is, isn't it?
Jon Skeet said:I think it's misleading to say that an interface is a class or a
struct. An interface is a distinct kind of type in itself. It may be
*implemented* by a class or a struct, but in itself, it is neither.
PS said:There is a difference between an interface as a .Net construct and interface
as the way in which we interact with an object.
You could sat that a class is it's own interface.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.