• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

Explain Interfaces

I am currently learning C#. I know C++ quite well, and a few of Java.
I thought a C# 'interface' was similar to an 'abstract' class when I suddenly discovered that there exists two independant keywords to declare classes : 'interface' and 'abstract'.
I don't really understand the notion of 'contract' in the definition of an interface.

Please explain C# interfaces, in what situation they should be used, and how they differ from abstract classes.
2 Solutions

an abstract class just serves as a base class to derive other classes from. Example: you could have an abstract class, named "building". Since it is abstract, you cannot make an instance of a building-object. You can however derive the classes "house,"farm",... from the class "building". If these are not abstract, it is possible to create a house or a farm object. So, basically an abstract class only contains the basics of yet-to-defne class.
On the otherhand, an interface defines the methods that a class, which implements this interface, should at least have. It can have additional methods but MUST have the ones defined in the interface. If the class "house" implements the interface "sortHouses" and this interface declares a method "sort", then the class house MUST have a method named "sort" as well. It is not stated in the interface what the method should do, or how it should do this, but it forces a class to implement a method.

More on this subject can be found quite easily by using google. Search for abstract class (MSDN can help) and interface.

The two are very similar with one very important difference: an abstract class can contain implementation whereas an interface just contains structure.  Basically, if you define an interface, IMyInterface like this:

interface IMyInterface {
   string GetName();
   int Number {

That just says that any class that impliments IMyInterface MUST contain a GetName method that returns a string and MUST contain a Number property with a getter (a setter could be included in the implementing class but wouldn't be accessible through the interface).  If, however, you wanted a standard way to control the Number property of all classes that implemented IMyInterface, you would need an abstract class:

abstract class MyAbstractClass : IMyInterface {
   abstract string GetName(); // the abstract keyword indicates that the derrived class MUST implement GetName itself
   int Number {
      get {
         return 5;

The difference is that IMyInterface leaves all implementation up to the derrived class whereas MyAbstractClass provides some of the implementation itself.  An interface is just like an abstract class that only contains abstract members.
Also note that you can only do multiple inheritance from interfaces, not abstract classes.

Last, I advice you to look at this URL: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconAbstractClassesVersusInterfaces.asp
stsanzAuthor Commented:

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now