The main reason is that Interfaces allow you to provide a common ground between your implementation and who ever is making use of your code. There are scenarios where you do not want the caller to know how you are executing certain pieces of logic. The reasons for this can be security or proprietary algorithms, to mention a few.
When you define an interface, you outline what functions you will be offering. In most cases, the caller will not care how you have implemented, say, a sorting mechanism. The caller simply cares that it works.
Coding to an interface also gives you a relatively large degree of freedom since you are, at no point, constraining yourself to implemented logic. So, if your method yields a List
instead of an ArrayList
, you are giving the freedom to the caller to resolve that to any class which implements list.
As @Benjamin Rogge said, I would also like to see where you read that abstract classes are more difficult to maintain.
I
inAPI
stands for: interface. Granted, that does not mean that all API's must be interface-only, but it does seem appropriate. – Brandon Jun 24 '14 at 14:15Standard ML
andOCaml
it's possible to have static dispatch while still substituting one module for another. The problem is that Java wants you to hard-code a unique identifier for the class into the source. – Doval Jun 24 '14 at 13:01