What is dependency injection and what are its benefits?
Answer
Dependency Injection (DI) provides dependencies to objects rather than objects creating them. Types: constructor injection (preferred, immutable), setter injection (optional dependencies), interface injection (rarely used). Benefits: loose coupling (depend on abstractions), testability (inject mocks), flexibility (swap implementations), adherence to SOLID. Frameworks: Spring (Java), Dagger (Java/Android), Angular (TypeScript). Example: PaymentService receives PaymentGateway via constructor instead of creating it. DI container manages object creation and lifecycle. Follows Dependency Inversion Principle.
Master These Concepts with IIT Certification
175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.