tags:

views:

170

answers:

5

abstract class can inherits by other class , and force people to override the abstract function, abstract property etc..

interface also can implements by other class, also force people to implements the function, property,indexers etc..

the only different i found is visual studio is smart to auto generate the nessesary member of this interface.

so , what's the different?

thanks you~

+1  A: 
  • You can provide partial implementation with an abstract class.
  • Interfaces don't smash the inheritance line; abstract classes do. You might want to mandate behaviour/capabilities without creating an is-a relationship.
Rob
Plus, an interface in c# is similar to a pure virtual class in C++.
KMan
A: 

An interface can have no implementation at all but an abstract class can implement methods and properties.

Jeremy
+1  A: 

In .NET, classes are limited to single inheritance but can implement any number of interfaces. If you inherit from an abstract class, that's the only one you can use. It may not be a big deal, but it would prevent you from perhaps inheriting from MarshalByRefObject down the road, as an example.

Also, the behavior defined by an abstract class is limited to the classes that inherit from it. On the other hand, behaviors defined by interfaces (IDisposable, IPrintable, etc.) can be applied across class hierarchies.

Matt Davis
Single inheritance is true for all .NET languages.
shoosh
Thanks for clarifying. I've updated the post.
Matt Davis
+4  A: 

Conceptually, I see a simple difference.

An interface defines expectations for a class. It declares that an implementor must provide certain behaviours (methods, properties, events). It doesn't prescribe how a class should work, merely what it should do.

An abstract class is a base class that cannot be instantiated on its own. Usually it provides a partial implementation that can be shared among concrete classes that derive from it. Thus it prescribes how a class should work.

This in turn leads to several practical differences because of language constraints (eg C# doesn't support multiple inheritance, except for interfaces). Which to use really depends on what you are trying to achieve.

Nader Shirazie
You don't inherit an interface, do you? I thought you implemented an interface... (Could be wrong though)
Svish
Yes you are correct. You inherit from a base class, and you implement an interface. Really, the two words serve to distinguish the nature of the base class / derived class relationship. I only described it that way so that I didn't get anyone jumping on me saying "C# supports multiple inheritance for interfaces". But I may be wrong here too...
Nader Shirazie
A: 

Adding to @Jeremy short and concise answer :) An abstract class would have the "abstract" and "virtual" keywords that would specify if a method or a property have an implementation. An abstract method/property would have to be implemented in the concrete class, but the virtual method/property may not be overridden in the concrete class since it already has an implementation in the abstract class...

I hope I made myself clear :))

Mina Tickylum