0

Does the Dependency Injection refer only to a case with Interfaces? Assume we have constructor of the class with parameter declared as Interface so we can pass implementation when creating a class. The same we can do when we create an abstract class and a class which extends it. Does the second case could be also named Dependency Injection or this technique refers only to interface usage?

padrian92
  • 127
  • 3
  • 15
  • 2
    "Dependency injection" is said even about injection of concrete classes declared as such at injection points. So why does the interface-typed injection point have to be special? – ernest_k May 31 '18 at 20:37
  • The canonical answer to this question is [here](https://stackoverflow.com/questions/130794/what-is-dependency-injection). In answer to the direct question, I've even seen "dependency injection" done with a single concrete class that implements variable behaviour based on its configuration. – DavidW May 31 '18 at 21:55

1 Answers1

0

Dependency Injection doesn't have a specific deal with interfaces, at all. In simplest terms, it just states, inject the damn dependencies at run-time as you want without being controlled by inbuilt compile-time dependencies.

It is analogical to something like this. I purchase a car with an engine I want without buying a car with an inbuilt engine (engine they want). So it is all about the run time configurability of object creation.

So there's no matter at all whether you are injecting parameter type being an interface, abstract class or even a concrete class.

But anyway though it has no business with DI, if you are using an interface or an abstract class instead of a concrete class type, you can introduce more generalization (via polymorphism) to your code.

Supun Wijerathne
  • 9,992
  • 6
  • 43
  • 67