Type Hinting and Dependency Injection
Learn about type hinting the injected objects and setting them using the setter methods.
We'll cover the following
Type hinting injected objects
Though type hinting is not necessary for dependency injection, it is preferred in situations when we want to check the dependency of the main class on types of objects. In our example, we expect any Driver
class that uses the Car
object to also have a sayYourName()
method. To do that, let’s create a Driver
interface and make both HumanDriver
and RobotDriver
classes implement the interface.
Get hands-on with 1400+ tech skills courses.