CS FundamentalsHardProduct-company design interview

Which form of dependency injection makes required dependencies explicit and allows the object to be immutable and fully initialised as soon as it is created?

  1. A. Setter injection
  2. B. Service-locator lookup inside methods
  3. C. Field injection by reflection
  4. D. Constructor injection

Correct answer: D. Constructor injection

With constructor injection an object cannot exist without its dependencies, and fields can be final. Setter injection suits optional dependencies; a service locator hides dependencies again.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic