When and where should I use an interface ?
4 Answers
Use an interface when you want to define behavior but not provide an implementation.

- 118,147
- 33
- 203
- 236
When you want to separate what's done (method signature definition) from how it's done (method implementation). This is common when you have operations that might be implemented in different ways, but users would acknowledge a common abstracton for all of them.
See java.util.Collection package for examples. There are several implemetations of the java.util.List interface, but the method signatures are the same.

- 305,152
- 44
- 369
- 561
It is strange nobody mentioned the word "contract" (though the previous answer describes it). See for example: Java interfaces - What exactly is in the contract? All classes implementing an interface fulfill that interface's contract. This contract is often the only thing a client needs to know about seemingly different classes.
And of course - polymorphism, which is a very convenient way to deal simultaneously with all classes implementing the interface. You just write code using only one supertype - interface's type.

- 1
- 1

- 1,785
- 3
- 21
- 33