Junior — Middle
In what situations and for what purposes do you use interfaces in your development?
sobes.tech AI
Answer from AI
Interfaces in Java are used to define a contract that classes can implement. This enables polymorphism and flexible architecture.
Main use cases for interfaces:
- When you need to specify a set of methods that different classes, not related by inheritance, should implement.
- To achieve multiple inheritance of behavior, since Java does not support multiple inheritance of classes.
- To facilitate testing and swapping implementations via dependency injection.
For example, the Runnable interface defines the run() method, which is implemented by different classes to run in a separate thread.