TIMEX
TIMEX

Reputation: 271684

When is an "interface" useful?

OOP interfaces.

Upvotes: 3

Views: 701

Answers (5)

Pranay Rana
Pranay Rana

Reputation: 176896

  1. An Interface totally abstracts away the implementation knowledge from the client.
  2. It allows us to change their behavior dynamically. This means how it will act depends on dynamic specialization (or substitution).
  3. It prevents the client from being broken if the developer made some changes to implementation or added new specialization/implementation.
  4. It gives an open way to extend an implementation.

Programming language (C#, java )

These languages do not support multiple inheritance from classes, however, they do support multiple inheritance from interfaces; this is yet another advantage of an interface.

Upvotes: 0

user253984
user253984

Reputation:

Basically Interfaces allow a Program to change the Implementation without having to tell all clients that they now need a "Bar" Object instead of a "Foo" Object. It tells the users of this class what it does, not what it is.

Example:

A Method you wrote wants to loop through the values given to it. Now there are several things you can iterate over, like Lists, Arrays and Collections.

Without Interfaces you would have to write:

public class Foo<T>
{
    public void DoSomething(T items[])
    {
    }

    public void DoSomething(List<T> items)
    {
    }

    public void DoSomething(SomeCollectionType<T> items)
    {
    }

}

And for every new iteratable type you'd have to add another method or the user of your class would have to cast his data. For example with this solution if he has a Collection of FooCollectionType he has to cast it to an Array, List or SomeOtherCollectionType.

With interfaces you only need:

public class Foo<T>
{
    public void DoSomething(IEnumerable<T> items)
    {
    }
}

This means your class only has to know that, whatever the user passes to it can be iterated over. If the user changes his SomeCollectionType to AnotherCollectionType he neither has to cast nor change your class.

Take note that abstract base classes allow for the same sort of abstraction but have some slight differences in usage.

Upvotes: 0

Gopi
Gopi

Reputation: 10293

In my own experience I find interfaces very useful when it comes to design and implement multiple inter-operating modules with multiple developers. For example, if there are two developers, one working on backend and other on frontend (UI) then they can start working in parallel once they have interfaces finalized. Thus, if everyone follows the defined contract then the integration later becomes painless. And thats what interfaces precisely do - define the contract!

Basically it avoids this situation : alt text

Upvotes: 18

Sameer Joshi
Sameer Joshi

Reputation: 87

Interfaces are very useful, in case of multiple inheritance.

Upvotes: 1

Heini H&#248;gnason
Heini H&#248;gnason

Reputation: 677

Interfaces are very useful when you need a class to operate on generic methods implemented by subclasses.

public class Person
{
  public void Eat(IFruit fruit)
  {
    Console.WriteLine("The {0} is delicious!",fruit.Name);
  }
}

public interface IFruit
{
   string Name { get; }
}
public class Apple : IFruit
{
  public string Name
  {
    get { return "Apple"; }
  }
}
public class Strawberry : IFruit
{
  public string Name
  {
    get { return "Strawberry"; }
  }
}

Upvotes: 2

Related Questions