?
Solved

Explain Interfaces

Posted on 2005-03-05
4
Medium Priority
?
418 Views
Last Modified: 2012-06-22
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.
0
Comment
Question by:stsanz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 9

Accepted Solution

by:
Wim_Bl earned 400 total points
ID: 13467153
Hi,

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.

greetings,
Wim
0
 
LVL 4

Assisted Solution

by:thedude112286
thedude112286 earned 100 total points
ID: 13467424
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 {
      get;
   }
}

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.
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 13469625
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
0
 
LVL 6

Author Comment

by:stsanz
ID: 13475388
Thanks!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question