RoR
RoR

Reputation: 16462

Interface usage question

Why is it that an interface seems to be exactly the same thing as the class but just the function declaration. What Are the major benefits to interface? Besides multiple classes sharing an interface but still it feels like what for? Each class will have it's own implementation thus why use an interface?

Upvotes: 2

Views: 121

Answers (7)

Luc M
Luc M

Reputation: 17304

This answer helped me to understand the interface usage.

I would like to upvote it once a day :-)

Upvotes: 1

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103135

Interfaces (in general) provide Abstraction and Encapsulation.

Abstraction means that the client does not need to know more than in the interface to use the object.

Encapsulation means that the client cannot know more than is in the interface.

Together these are called information hiding.

So, to an extent, you are right that each class provides its own implementation so you do not need the interface. However, in that case the client becomes dependent on the implementation. Unfortunately, in the real world, implementation changes all the time. As implementation changes, all dependent code will have to be changed.

By using an interface, the client only depends on what is specified in the interface and not on the actual implementation. So the implementation may change and the client code may not necessarily require changing. Of course care has to be taken that the interface itself does not change.

Say you had a collection of objects. You then needed those objects sorted in some order. If you implemented your collection as a Tree then the code to sort is completely different from if you implemented a linked list. The client code, being exposed to these implementations would require changing if any details of these implementations changes.

If on the other hand you create an Interface, say Collection, that promises that any implementation would be able to sort itself but it does not tell you how it will sort, then your client code can depend on the ability to sort without knowing the details of sorting.

You can happily change the Collection type and the sorting algorithm without affecting the client code.

Upvotes: 0

Stephane Rolland
Stephane Rolland

Reputation: 39896

When designing with interfaces, one underlines the behaviours expected without polluting with any implementation futile details.

An interface represents a contract, allowing the rest of the application to use through this interface any object that abide to this contract.

In a nutshell:

  1. Clarity of design
  2. Flexibility of design.

Upvotes: 0

GolezTrol
GolezTrol

Reputation: 116100

It allows two totally unrelated classes to implement the same interfaced, and as such can be used in the same way. (Like calling ISortable.Sort on two lists with a completely different implementation. It's like multiple inheritance, only better. :)

And interfaces are used for sharing objects between processes as well (Google for COM or Automation).

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499770

Using an interface means that the callers of the interface aren't tied to a specific implementation.

For example, if you're using C# and you need a sequence of data, you expect to see that in terms of IEnumerable<T>. You don't care what the implementation is - and requiring one specific implementation would limit how your code could be used.

Another example would be Stream - which is an abstract class rather than an interface, but the basic principle is the same. You can parse any kind of stream into an XmlDocument (to pick an arbitrary use of streams). It doesn't matter whether it's a FileStream, a NetworkStream, a MemoryStream or anything else, so long as it supports the required operations.

Upvotes: 1

hungryMind
hungryMind

Reputation: 6999

Interface normally used when you need to interact between different sub-systems. It also gives benefit of loose coupling and hide actual implemented objects.

Upvotes: 0

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76778

In statically typed object oriented languages, interfaces make it possible to use classes that only share the interface to be used polymorphicaly.

Upvotes: 0

Related Questions