The End
Now that we discussed the What, Why and How of Dependency Injection in Java at great length, let us see where the idea originated from. You’ve got it right, this was from the SOLID principles, which is an acronym for the first five object-oriented design (OOD) principles by Robert C. Martin, introduced in his 2000 paper “Design Principles and Design Patterns”. You can refresh the concepts of SOLID principles here.
The 5th alphabet of SOLID represents “Dependency Inversion” principles, which focuses on the approach where the higher classes are not dependent on the exact implementation of lower classes but instead depend upon the abstraction of the lower classes.
This aim to reduce the coupling between the classes is achieved by introducing abstraction between the layers, thus it doesn't care about the real implementation. So, when the higher class needs to use a method of the lower class, it will use the methods available in the abstraction and get freed of the dependency on a specific implementation.
In a similar school of thought, another important rule for coding standards is to choose composition over inheritance to have a flexible design. You can read more about this here.
Curious Cats:
How can we do dependency injections in languages like C & C++, which lacks the level of Abstraction support like that in Java?
One-liner: In C & CPP, we use function pointers to achieve Dependency injection.
Detailed read:
https://www.codymorterud.com/design/2018/09/07/dependency-injection-cpp.html
Further Good Reads:
https://www.baeldung.com/spring-annotations-resource-inject-autowire
https://betterprogramming.pub/prefer-composition-over-inheritance-1602d5149ea1
https://www.educative.io/edpresso/what-are-the-solid-principles-in-java
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article