-4

Is there any valid reason that we should use interfaces over polymorphism? Every video, resource and everything else I searched just talks on how it's good to have interfaces to follow rules (contracts) or to just follow polymorphic behaviour.

Can anyone expand? It seems nobody else can... I'm specifically looking at why interfaces are used for decoupling and how they can help with that.

Nayantara Jeyaraj
  • 2,624
  • 7
  • 34
  • 63
isolation
  • 3
  • 1
  • 1
    I have read hundreds of blog posts and books on this topic, and even podcasts explain it. Lots of people can. Unfortunately, opinion and explanation aren't the goal of StackOverflow. Try https://softwareengineering.stackexchange.com – Tim Mar 21 '18 at 04:06
  • Possible duplicate of [C# interfaces - What's the point?](https://stackoverflow.com/questions/6802573/c-sharp-interfaces-whats-the-point) – Wyck Mar 21 '18 at 04:10
  • Often, interfaces reduce the exposed interface of an object/class. This in itself increases cohesion. One way of thinking of cohesion, perhaps a little over-simplistic way, is as the proportion of the use of an interface. Patterns, such as a Facade pattern, can be implemented using an interface. – MEC Mar 26 '18 at 07:48

2 Answers2

1

Interfaces Formalise polymorphism

Interfaces allow us to define polymorphism in a declarative way, unrelated to implementation. Two elements are polymorphic with respect to a set of behaviours if they realise the same interfaces.

In regards to decoupling

Classes can share the same Footprint / Interface and live in totally different places in the world and need not know about each other, hence decoupled.

Coupling (computer programming)

In software engineering, coupling is the degree of interdependence between software modules; a measure of how closely connected two routines or modules are;[ the strength of the relationships between modules.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
0
  1. You can’t substitute a test stub

  2. A class can’t have multiple base classes but can have multiple interfaces

  3. Classes can’t have generic variance, only interfaces and delegate types can.

  4. Sometimes you want to declare an interface without an implementation, e.g. so two different apps can agree on a contract defined in code.

John Wu
  • 50,556
  • 8
  • 44
  • 80