tags:

views:

198

answers:

6

Possible Duplicate:
Interfaces: Why cant I seem to grasp them?

What is the purpose of interfaces in C#?
How would they enable extendable,modular design in c#,Java?

As far as my experience with the interfaces is concerned ,we used in a gridview scenario where columns values are brought from disparate objects.

(e.g:

List<IPub> list = new List<IPub>();
gridview.DataSource = list;
gridview.DataBind();

IPub has 4 methods which is implemented by 4 or 5 disparate classes. ) What are the cases they come in handy compared to their class counterparts,apart from above?

I heard Java creator despised of interfaces or saying like "If i were a given a chance to design java again;I would never make interfaces into the language". Does this applies to C# as well? What implications made him to say that ? I am feeling like i never understood interfaces completely. Please somebody take pains to explain.
EDIT: Here is the Goslings quote, see the Java section

+1  A: 

you should see the answers here or here or here or here

An interface provides a way to use something without having to worry about how that thing is implemented.

If you have an interface ITextGetter with a method GetText(string textKey) which you are using, you don't know if where the text comes from, all you know is that when you ask for the text with a particular key, you get the text. If you write you app using this interface then you can use different classes which all implement the interface to get the text without having to change your app. This makes it easy to switch from file based text getting to database based or webservice based text getting without having to change any of the code that uses the interface

Sam Holder
+1  A: 

Interfaces are a sort of contract: if some class implements some interface, then that class guarantees to have certain methods. You could have the same when you inherit from some baseclass, but you can't always do that. In that case an interface is handy to implement a sort of multiple inheritance.

Hans Kesting
+1  A: 

Interfaces are there so that you don't need to specify a whole lot of functional code about something. You just need to know what it does. It's kinda like if you need to hire a family car.

You need to know
A) its a car that drives and
B) it has a family sized boot.

You don't need to know about how the car works, how the engine works. You just need to do know it can do A) and B). That's the purpose of an interface.

Within C# we don't allow multiple inheritance, (i.e. having more than 1 base class), so if you want to define the behaviour of more than 1 interface you can't do it with classes. If multiple inheritance existed there would be the option of using multiple base classes to do this.

Ian
+1  A: 

Well, back when java was being developed there was a thing called "Multiple inheritance" where classes could be derived from two (or more) different classes.

So for example, you had a "Vehicle" class and a "Animal" class you could have a "Horse" class that derived from both.

The problem was, what if you wanted to derive a class from two classes that had a function name in common? You couldn't really, there would be two underlying implementations of the function.

So for example the Vehicle and Animal classes might have a "move()" function. Well, if you don't override that yourself, then what happens when you call horse.move()? It's undefined! Either one could get called!

So, in Java, you can only derive from one class. But what if you really need a class that is compatible with more then one type? Like if you have, for example a database connection that needs to derive a base class to work with the DB but also needs to be able to be managed like a resource with a close() function.

Okay, so they created 'Interfaces'. When you implement an interface, you don't have to worry about competing code underneath because you have to implement them yourself.


It's generally agreed that multiple inheritance is a "considered harmful" (like GOTO), and interfaces are a way of getting the benefits without the downsides. But, nowadays there are other ways to do that. Such as "DuckTyping" where as long as classes implement the feature name, they are good to go.

Also, java now can do nested classes, so if you need your class to be derived from two classes, you can make an inner class that derives from the second class.

Different methods have their pluses and minuses, but interfaces are not the only want to get around the problems of multiple inheritance today.

Chad Okere
+1  A: 

Interfaces give you a way to effectively have some sort of multiple inheritance (you can't inherit from more than one abstract base class). If you ask yourself the question why would you prohibit multiple class inheritance, just read relevant chapter from one of Bjarne Stroustroup's books (on C++), where you will how overcomplicated it gets.

On the other note, when you are using unit testing, pretty much every interface you create will have at least 2 implementations - the real one and a mocked one for the tests.

Grzenio
+3  A: 

Interfaces can be considered as "can-do", whilst abstract/base classes can be considered as "is-a".

So a dog is-a animal, so you'd used a base class. A dog can-do a poo, so poo would be a method on an interface which other animals might implement.. (crap example)

Reading my OO book the other day it posited that concrete instances should be avoided and good OO coders always program to an interface or base-class.

With that in mind it might be worth picking up a copy of a good OO / patterns book, such as head first design patterns

Antony Koch
A good way to look at it. +1 for the poo pun!
Mongus Pong
I'd recommend that book too - very clear outline.
Paddy
I recommend it as well.
Clean