0

There is no multiple inheritances. But my project partner used interface for implementing add, remove etc methods.

Here is code:

public interface IAccountCategoryDataSource
{
    bool Add(AccountCategory accountCategory);
    bool Update(AccountCategory accountCategory);
    bool Remove(AccountCategory accountCategory);

    AccountCategory GetById(int id);
    AccountCategory GetByName(string name);
    IEnumerable<AccountCategory> GetByParentCategory(AccountCategory category);
    IEnumerable<AccountCategory> GetTopLevelCategories();
    IEnumerable<AccountCategory> GetBySearchTerm(string searchTerm);
    IEnumerable<AccountCategory> GetAll();

    event EventHandler<ObjectAddedEventArgs> AccountCategoryAdded;
    event EventHandler<ObjectUpdatedEventArgs> AccountCategoryUpdated;
    event EventHandler<ObjectRemovedEventArgs> AccountCategoryRemoved;
}

please explain what is the need of interface.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pratik
  • 167
  • 3
  • 11
  • 1
    What do you mean by "multilevel inheritances"? – Jon Skeet Apr 15 '12 at 08:08
  • I don't understand your question. An interface represents a contract, and here it is likely used to decouple the contract from its implementation. Possibly the implementation is even in an assembly not referenced by the consumers of this interface. – CodesInChaos Apr 15 '12 at 08:10
  • as my knowledge ,i used interface only in multilevel inheritance situation .but,here i did not see any multilevel inheritance situation .so,i can not understand interface role. – Pratik Apr 15 '12 at 08:18
  • 1
    @user1334247: You still haven't explained what you mean by "multilevel inheritance". – Jon Skeet Apr 15 '12 at 08:22
  • @codeinchasos can u give advantage of decouple the contract from its implementation. ?plz – Pratik Apr 15 '12 at 08:27
  • @jon skeet : sorry interface is uesd for implementing multiple inheritances. ab=nd code for multiple inhe... http://stackoverflow.com/questions/178333/multiple-inheritance-in-c-sharp – Pratik Apr 15 '12 at 08:32
  • 1
    @user1334247: Okay - you should be aware that "multilevel inheritance" is *not* standard terminology for implementing multiple interfaces. And most of the time interfaces are used *without* multiple interfaces being implemented by the same type. – Jon Skeet Apr 15 '12 at 08:34

1 Answers1

0

Interfaces can be used for many purposes. Multiple inheritance or solving circular reference are few of them.

But in most cases, interfaces are used to make contract between consumer (class that needs some functionality) and implementation (class that implements this functionality). That means, that they both agree on WHAT this functionality is, but NOT HOW this functionality will be implemented. Consumer then doesn't need to care about implementation (consumer just uses arranged interface) and implementator can be sure, that when he implements all methods of interface correctly, any consumer of this interface will accept this implementation. This is especially useful, if consumer and implementation classes is written by different persons, but it is used even if it is not needed just to emphasize the fact, that consumer doesn't depend on particular implementation, which is very good practice in object oriented programming. There can be many different consumers of interface, there can be many different implementations of interface, but they don't need to know anything about each other, because they communicate only through interface.

To give you some example, imagine that you are working on class that has method which returns 5 biggest integers from list of integers. The simplest method how to do it is to sort whole list in descending order and then return first 5 numbers. But you don't want to implement sorting algorithm in your class, because it is separate functionality and should be implemented in other class (or maybe there already is class that do that). So you define interface with one method Sort, that takes array of integers and returns sorted array. You don't care how this sorting function will be implemented, you just use Sort method of this interface and you can finish your class even without knowing anything about sorting algorithms. Then your co-worker (or you) will create another class, that implements this interface. He doesn't need to know anything about your class, he can use any sorting algorithm he wants, he can change algorithm anytime in the future, and it will still work together.

Good example of necessity of interfaces are plug-ins. Author of main application creates publicly available interface with functions, that his application will use. Whoever then writes class with implementation of this interface can use it as plug-in.

Community
  • 1
  • 1
Ňuf
  • 6,027
  • 2
  • 23
  • 26