My question is simple: is there any advantage of using interfaces if they are implemented by a single class ?
I always thought that interfaces are good only when there are multiple implementations of that interface.
Thanks.
Another welcome benefit of Java 8 Interfaces is the ability to add new concrete methods to an existing interface, which has already been implemented, without breaking the program. Before Java 8, if you had a class implementing an interface but not using all its methods, that class would have to be labeled as abstract.
Using an interface is far more powerful than implementing one method in multiple classes, because if each class implements the same interface, you can treat them all the same (without casting).
In a word: no. The contract that an interface means can be specified directly in your only class.
If you are clear that you won't need another implementation of the same methods in the future, you can avoid defining an interface.
Of course, the issue here is "in the future" clause. If the project is small, without a long development/upgrade period, and well defined, you can be almost sure about what will be needed in the future.
If the project is long as is probably that it will be subjected to changes, then you will have to factor in:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With