-1

Possible Duplicate:
Why to use Interfaces ? Multiple Inheritance vs Interfaces ? Benefits of Interfaces?

I want to know why do we use interfaces as they contain only declaration of methods. Not any code...

Then, what are the benefits of using interfaces? Why to implement them? Why to use them?

Community
  • 1
  • 1
Darwin
  • 21
  • 3
  • 1
    possibly duplicate of http://stackoverflow.com/q/8531292/1055241 look at the accepted answer to get idea of using interfaces – gprathour Jan 27 '12 at 07:54
  • Even if the downvotes are just for the duplicate question, the referred to question - just judging from it's title - might not have been obvious to the OP (him looking for the logic/meaning of interfaces). Anyways, while this is a newbie question, there seems to be nothing wrong with it, other than something similar having been asked before. So could all those downvoters please add a helpful comment, on why this question is unclear or not useful? Thx. – Levite Dec 17 '14 at 09:39
  • Interfaces are not meant to actually do something, they are rather used to describe and guarantee what an implementing class is capable of doing! For example an interface named `IEatable` could have a method `eat()`, which concrete classes like `Cookie`, `Hamburger`, or `Spinach` could implement. You can therefore be sure that those classes will each have a method `eat()`. But calling it for `Cookie` might print "Yum yum!", whilst `Spinach` just outputs "Yuk!". – Levite Dec 17 '14 at 10:05

1 Answers1

1

You already mentioned the point: Because you then code against objects that implement those interfaces, instead of the concrete object-class. Advantage: Your code will work with any implementation that implements this interface.

You are basically hiding implementation, saying: "I dont care about the concrete implementation, as long as the object is offering this interface"..

quaylar
  • 2,617
  • 1
  • 17
  • 31