473,396 Members | 1,724 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,396 software developers and data experts.

interfaces

Can someone tell me whats the exact advantage of Interfaces in C#. To me
abstract classes serves the purpose and have all good things then what is
special about interfaces...can someone tell me some real example from
system, user, developer point of view....

Jun 7 '07 #1
18 1742
abcd wrote:
Can someone tell me whats the exact advantage of Interfaces in C#. To me
abstract classes serves the purpose and have all good things then what is
special about interfaces...can someone tell me some real example from
system, user, developer point of view....
You can implement many interfaces in a class but only have one single
base-class.

Also an interface doesn't force you to make the methods virtual/override.

Additionally all methods for an interface have to be public.
Jun 7 '07 #2
On Jun 7, 4:12 pm, "abcd" <a...@abcd.comwrote:
Can someone tell me whats the exact advantage of Interfaces in C#. To me
abstract classes serves the purpose and have all good things then what is
special about interfaces...can someone tell me some real example from
system, user, developer point of view....
Try reading this thread:

http://groups.google.com/group/micro...667cc111cc960b

Jun 8 '07 #3
On Jun 8, 5:16 am, Bruce Wood <brucew...@canada.comwrote:
On Jun 7, 4:12 pm, "abcd" <a...@abcd.comwrote:
Can someone tell me whats the exact advantage of Interfaces in C#. To me
abstract classes serves the purpose and have all good things then what is
special about interfaces...can someone tell me some real example from
system, user, developer point of view....

Try reading this thread:

http://groups.google.com/group/micro...languages.csha...
Basicallly we need interfaces when there is a need to have similiar
methods with different implementations spanning across multple
classes. If you are dealing with implementation in one class then
abstract classes are enough. But when we need different implementation
for similiar methods we can use interfaces. For e.g consider
peripheral properties - we have Move() method for Vehicle and Animal.
But the implementations are different for the same methd Move() in
both classes.

Jun 8 '07 #4
abcd wrote:
Can someone tell me whats the exact advantage of Interfaces in C#. To me
abstract classes serves the purpose and have all good things then what is
special about interfaces.
The two ideas say very different things. Inheriting from an abstract
class simply says that your class "is a ..." whereas implementing an
interface says "my class will do ..."

As mentioned by Cody elsewhere in this thread, C# doesn't support
multiple inheritance, so you can only inherit from one abstract class at
a time; but you can implement as many interfaces as you like.

For example, if you are creating a "Dog" class you might inherit from
the abstract class "Animal" and implement the interfaces "IBark" and
"IChaseParkedCars"; but you wouldn't be able to do that if "IBark" and
"IChaseParkedCars" were abstract classes rather than interfaces.

--
Dylan Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.
Jun 8 '07 #5
Hi,

"abcd" <ab**@abcd.comwrote in message
news:OJ*************@TK2MSFTNGP06.phx.gbl...
Can someone tell me whats the exact advantage of Interfaces in C#. To me
abstract classes serves the purpose and have all good things then what is
special about interfaces...can someone tell me some real example from
system, user, developer point of view....
Since you can only have one base class Interfaces is what allow a class to
implement more than one ,well interface :)
Jun 8 '07 #6
On Jun 7, 4:12 pm, "abcd" <a...@abcd.comwrote:
Can someone tell me whats the exact advantage of Interfaces in C#. To me
abstract classes serves the purpose and have all good things then what is
special about interfaces...can someone tell me some real example from
system, user, developer point of view....
An interface will allow you to publish a standard, um, interface, for
making use of a functionality. For instance, you may write a sorting
library, and you want it to be generally available to other classes.
You publish an interface, consisting of a set of functions, that
classes have to implement in order to be able to use your library. Or
if you want your class to be able to implement BeginInvoke and
EndInvoke, you have to implement the ISynchronizeInvoke interface.
Implementing just means you have to supply a version of the functions
defined by the interface within your class.

..NET implements a number of interfaces, all of which start with "I",
as in IComparable, ICollection, etc. Classes that implement the
interface can use the functionality.

So if you are writing a class "smee" that you want to be able to sort,
use asynchronous communications, and serialize to a file (or
something), you'd define it as

class smee : IComparable, ISynchronizeInvoke, ISerializable {}

and implement all the required methods for those interfaces, then you
can hand a smee to any method that requires one of those interfaces.
Jun 8 '07 #7

"Barfy the Wonder Camel" <go******@yahoo.cawrote in message
news:11**********************@m36g2000hse.googlegr oups.com...
On Jun 7, 4:12 pm, "abcd" <a...@abcd.comwrote:
>Can someone tell me whats the exact advantage of Interfaces in C#. To me
abstract classes serves the purpose and have all good things then what is
special about interfaces...can someone tell me some real example from
system, user, developer point of view....

An interface will allow you to publish a standard, um, interface, for
making use of a functionality. For instance, you may write a sorting
An abstract class allows this as well.
library, and you want it to be generally available to other classes.
You publish an interface, consisting of a set of functions, that
classes have to implement in order to be able to use your library. Or
if you want your class to be able to implement BeginInvoke and
EndInvoke, you have to implement the ISynchronizeInvoke interface.
Implementing just means you have to supply a version of the functions
defined by the interface within your class.

.NET implements a number of interfaces, all of which start with "I",
as in IComparable, ICollection, etc. Classes that implement the
interface can use the functionality.

So if you are writing a class "smee" that you want to be able to sort,
use asynchronous communications, and serialize to a file (or
something), you'd define it as

class smee : IComparable, ISynchronizeInvoke, ISerializable {}

and implement all the required methods for those interfaces, then you
can hand a smee to any method that requires one of those interfaces.
Again, an abstract class works the same, except that abstract classes are
far more powerful. However, .NET limits base classes to single inheritance,
but allows multiple interfaces in the base list.
Jun 8 '07 #8
"Dylan Parry" <us****@dylanparry.comwrote in message
news:46*********************@news.gradwell.net...
>
The two ideas say very different things. Inheriting from an abstract
class simply says that your class "is a ..." whereas implementing an
interface says "my class will do ..."
I think both are is-a relationship. If you implement IDisposeable, then your
class is-an IDisposeable, in the Liskov sense of being about to use objects
of that class whenever an IDisposeable is expected.

///ark
Jun 8 '07 #9
On Jun 8, 10:19 pm, "Mark Wilden" <mwil...@communitymtm.comwrote:
"Dylan Parry" <use...@dylanparry.comwrote in message

news:46*********************@news.gradwell.net...
The two ideas say very different things. Inheriting from an abstract
class simply says that your class "is a ..." whereas implementing an
interface says "my class will do ..."

I think both are is-a relationship. If you implement IDisposeable, then your
class is-an IDisposeable, in the Liskov sense of being about to use objects
of that class whenever an IDisposeable is expected.

///ark
I don't think both are the same - "is" relation between base class-
>derived class and interface->implemented class.
In inheritance it would seem ok. But in case of interfaces it's more
like "implements" some methods.

Jun 8 '07 #10
On Jun 8, 11:16 pm, "Aneesh Pulukkul[MCSD.Net]" <anees...@gmail.com>
wrote:
On Jun 8, 10:19 pm, "Mark Wilden" <mwil...@communitymtm.comwrote:
"Dylan Parry" <use...@dylanparry.comwrote in message
news:46*********************@news.gradwell.net...
The two ideas say very different things. Inheriting from an abstract
class simply says that your class "is a ..." whereas implementing an
interface says "my class will do ..."
I think both are is-a relationship. If you implement IDisposeable, then your
class is-an IDisposeable, in the Liskov sense of being about to use objects
of that class whenever an IDisposeable is expected.
///ark

I don't think both are the same - "is" relation between base class->derived class and interface->implemented class.

In inheritance it would seem ok. But in case of interfaces it's more
like "implements" some methods.
MSDN recommendations:
http://msdn2.microsoft.com/en-us/lib...1d(VS.71).aspx

Jun 8 '07 #11
"Aneesh Pulukkul[MCSD.Net]" <an******@gmail.comwrote in message
news:11*********************@q75g2000hsh.googlegro ups.com...
On Jun 8, 10:19 pm, "Mark Wilden" <mwil...@communitymtm.comwrote:
>"Dylan Parry" <use...@dylanparry.comwrote in message

I don't think both are the same - "is" relation between base class-
>>derived class and interface->implemented class.
In inheritance it would seem ok. But in case of interfaces it's more
like "implements" some methods.
To a user of an object, who is expecting an IInterface, any object that
implements IInterface is the same as any other.

Also remember that every class defines an interface. The C# "interface"
construct simply says that that's -all- it defines.

The important point of is-a (in my mind) is substitutability.

///ark
Jun 8 '07 #12
On Jun 8, 12:35 pm, "Mark Wilden" <mwil...@communitymtm.comwrote:
"Aneesh Pulukkul[MCSD.Net]" <anees...@gmail.comwrote in message

news:11*********************@q75g2000hsh.googlegro ups.com...
On Jun 8, 10:19 pm, "Mark Wilden" <mwil...@communitymtm.comwrote:
"Dylan Parry" <use...@dylanparry.comwrote in message
I don't think both are the same - "is" relation between base class-
>derived class and interface->implemented class.
In inheritance it would seem ok. But in case of interfaces it's more
like "implements" some methods.

To a user of an object, who is expecting an IInterface, any object that
implements IInterface is the same as any other.

Also remember that every class defines an interface. The C# "interface"
construct simply says that that's -all- it defines.

The important point of is-a (in my mind) is substitutability.

///ark
Sorry, I strongly disagree.

The important point of "is-a" is classification.

The important point of an interface is to establish a contract or a
capability, for substitutability (as you said) or polymorphism
(another word for same).

Jun 8 '07 #13

"Mark Wilden" <mw*****@communitymtm.comwrote in message
news:eh**************@TK2MSFTNGP03.phx.gbl...
"Aneesh Pulukkul[MCSD.Net]" <an******@gmail.comwrote in message
news:11*********************@q75g2000hsh.googlegro ups.com...
>On Jun 8, 10:19 pm, "Mark Wilden" <mwil...@communitymtm.comwrote:
>>"Dylan Parry" <use...@dylanparry.comwrote in message

I don't think both are the same - "is" relation between base class-
>>>derived class and interface->implemented class.
In inheritance it would seem ok. But in case of interfaces it's more
like "implements" some methods.

To a user of an object, who is expecting an IInterface, any object that
implements IInterface is the same as any other.

Also remember that every class defines an interface. The C# "interface"
construct simply says that that's -all- it defines.

The important point of is-a (in my mind) is substitutability.
That, and interface implementation *is* inheritance.
>
///ark

Jun 8 '07 #14
"Bruce Wood" <br*******@canada.comwrote in message
news:11*********************@r19g2000prf.googlegro ups.com...
The important point of "is-a" is classification.
My feeling is that if one type "is-a" nother type, then the first type can
be substituted for the second. Does this run afoul of other definitions of
"is-a"?

Classification, on the other hand, means "is-like-a," because if a type
really "was-a" nother type, then they'd be the same type. "Is-a" really
means "can-be-used-as-a," otherwise, there'd be no practical reason for it.

///ark
Jun 12 '07 #15
"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message news:%>>
>The important point of is-a (in my mind) is substitutability.

That, and interface implementation *is* inheritance.
I don't follow. Clearly, implementations of an interface are substitutable
for each other. But they're not related by inheritance.

///ark
Jun 12 '07 #16
On Jun 12, 3:11 pm, "Mark Wilden" <mwil...@communitymtm.comwrote:
"Bruce Wood" <brucew...@canada.comwrote in message

news:11*********************@r19g2000prf.googlegro ups.com...
The important point of "is-a" is classification.

My feeling is that if one type "is-a" nother type, then the first type can
be substituted for the second. Does this run afoul of other definitions of
"is-a"?
I don't know about "runs afoul," but I'm not sure what it means. It
doesn't sound right to me, but then I'm struggling to imagine the
precise meaning of it.

"is-a" to me implies classification into more and more general
categories, or, going in the other direction, specialization into more
and more specific types. So, for example, a Duck "is-a" Bird, in the
sense that the statement "All ducks are birds" is an accurate one.

Interfaces do not imply this sort of relationship. If you define an
interface that is then implemented by many diverse classes, you cannot
say that the all members of the class "are" of that interface. Usually
one talks in terms of capabilities, and the interface names reflect
that, so "All ducks can quack," or "All ducks can fly."

Jun 13 '07 #17
"Bruce Wood" <br*******@canada.comwrote in message
news:11*********************@g37g2000prf.googlegro ups.com...
If you define an
interface that is then implemented by many diverse classes, you cannot
say that the all members of the class "are" of that interface.
As far as clients of that interface are concerned, all classes that
implement the interface are the same.

It seems to me that the only difference between an abstract class and an
interface is that an abstract class can contain implementation, and an
interface cannot. There's also multiple inheritance, and the fact that an
interface can't derive from a class, but I don't think that's germaine to
"is-a." Is there another difference that I'm missing, that would relate to
"is-a"? I don't mean a difference in intent, but a difference in what the
two constructs actually do.

I think it's important to note that all classes define an interface, hence a
contract that must be maintained by any derivative. In that sense, I would
say that a class "is-a" interface. :)

///ark
Jun 13 '07 #18
Mark Wilden <mw*****@communitymtm.comwrote:

<snip>
It seems to me that the only difference between an abstract class and an
interface is that an abstract class can contain implementation, and an
interface cannot. There's also multiple inheritance, and the fact that an
interface can't derive from a class, but I don't think that's germaine to
"is-a." Is there another difference that I'm missing, that would relate to
"is-a"? I don't mean a difference in intent, but a difference in what the
two constructs actually do.
Two extra differences which don't affect the "is-a" but should be on
the list for completeness:

1) Structs can implement interfaces
2) Abstract classes can't be proxied by RealProxy unless they derive
from MarshalByRefObject (IIRC)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 13 '07 #19

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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,...
0
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...

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.