What is the advantage of using an interface?

What is the use of an interface?

I heard that it is used instead of multiple inheritance, and also hide data.

Is there another advantage where the place interface is used, and how can a programmer define this interface?

What is the difference between explicit interface implementation and implicit interface implementation ?

+4
source share
7 answers

To solve the implicit / explicit question, let's say that two different interfaces have the same declaration:

 interface IBiographicalData { string LastName { get; set; } } interface ICustomReportData { string LastName { get; set; } } 

And you have a class that implements both interfaces:

 class Person : IBiographicalData, ICustomReportData { private string lastName; public string LastName { get { return lastName; } set { lastName = value; } } } 

The Person class implicitly implements both interfaces because you get the same output with the following code:

 Person p = new p(); IBiographicalData iBio = (IBiographicalData)p; ICustomReportData iCr = (ICustomReportData)p; Console.WriteLine(p.LastName); Console.WriteLine(iBio.LastName); Console.WriteLine(iCr.LastName); 

However, to implement it explicitly, you can change the Person class as follows:

 class Person : IBiographicalData, ICustomReportData { private string lastName; public string LastName { get { return lastName; } set { lastName = value; } } public string ICustomReportData.LastName { get { return "Last Name:" + lastName; } set { lastName = value; } } } 

Now the code:

 Console.WriteLine(iCr.LastName); 

There will be a prefix "Last Name:".

http://blogs.msdn.com/b/mhop/archive/2006/12/12/implicit-and-explicit-interface-implementations.aspx

+5
source

Interfaces are very useful for

  • Dependency Injection
  • Control inversion
  • Insulation testing
+2
source

The interface just separates the class API description from its implementation. This concerns the separation of issues that are fundamental to any reliable software project. You can replace implementation classes without breaking any other code.

One area where this is particularly useful is unit testing, as it allows you to spoof interfaces you don't want to test as part of this test case.

The presence of completely unrelated classes implements the same interface, also allows you to write methods that can work on different classes in different hierarchies (i.e. there is no common ancestor other than an object), without having to use the object as its type. For example, you can write a method that accepts IEnumerable, and pass it List, Array, etc. Without interfaces or a common base type, this would be impossible (except for casting from an object).

+1
source

In most basic terms, we return to OOP 101:

Inheritance: object B "is the type of object A. The behavior and methods implemented by object A are inherited, implemented, and all (with some room for redefinition) by object B.

Interface: Object A and Object B are both examples of the "Act Like" abstract object represented by a common interface. James Gaunt uses an example if Ienumerable is higher. Other examples may be IPrintable, IDisposable, Etc.

For any given class that implements these interfaces, the implementation may be completely different (think about how you embed IDisposable in different classes that use the dispose method). However, the client code does not need to know or care about what the actual type of the object is - the code can simply access the desired properties and methods through the interface.

Inheritance is often seen as a β€œmagic” response to many coding problems, but it is also widely used incorrectly as a means of avoiding redundant code. I disagree with user492238 that everything that is done with interfaces can just as easily be done through inheritance. This approach will often hit you in the corner. And, as Jodrell observes, multiple inheritance is not a .net feature (and rightfully, in my opinion).

When you find that you are implementing the same behavior in several (or many) unrelated classes, consider defining the interface that the API provides for this behavior. You can have several classes: Person, Animal, Building, etc. For each of them, you may need a method for printing. You may also have a method that takes IPrintableObject as a parameter. In this case, you can implement IPrintableObject in any of the classes that you want to print, provide implementation code in each of these objects, and pass them to the client.

+1
source

Most - if not all - of what can be done with interfaces can be accomplished through inheritance. Interfaces can be used to replace class design using abstract base classes. Which is preferable is mainly a matter of personal experience and taste. (Of course, there are people who maintain very strict rules about when to prefer one over the other).

And there are frameworks (often in combination with DI repository) that force you to use interfaces.

0
source

An interface is a contract; this ensures that the specified methods and properties are available. It does not provide an implementation, which distinguishes the class that provides the implementation.

Interfaces are the highest level of abstraction; it does not provide implementation details to the consumer.

Interfaces are almost semantically equivalent to pure abstract classes (classes that provide no implementation). In C #, vb.net, and other languages, classes can have multiple interfaces, but only one base class. Thus, for a particular advantage, interfaces have classes (abstract or others) - this is something that you can implement multiple interfaces, but only inherit from one class.

0
source

This is more a question for more experienced programmers than an answer, but ... Most of the time I worked with a small team, and we were close, knowing each other well. And I was wondering what interfaces are. But my last project was with a new team and there was a bit more. And it was a trifle mess. And after a while I came to the conclusion: we should use interfaces.

Not as a way to solve any technical problems, but as a kind of project document that is in the code that forces you to follow this design, which is difficult to ignore, which will simplify cooperation 100 times. It’s not a problem to sit down and come up with an interface. And then you can share and implement the material alone. But if you follow the interface, then every other person in the team can read the idea of ​​the class much more clearly.

So, I think that interfaces are a really good way to organize cooperation: implement, but you want, but no open interface outside interfaces and rewriting interfaces should not be performed with other participants. An easy way to keep the code in order and to separate ordinary things, and yours only things.

0
source

Source: https://habr.com/ru/post/1348069/


All Articles