Using interfaces in APIs - II
13 September 2008There are few good things with using interfaces in APIs. Ill shed some light on those.
If the interface is intended to be implemented by user code, then it is often more appropriate than an abstract class. This positive point remains as long as interface has just one method. If there are several methods and an implementation class only needs to do something in one of them, then it’s annoying to implement all the others. Another negative is that if an interface has 3 methods today and you add one method to it tomorrow, then all the implementations will be revised.
Interfaces prove useful to avoid multiple inheritance.
The java.lang.reflect.Proxy class allows you to make an implementation of any interface at runtime, where calling any of the interface’s methods results in a call to a single invoke method. You may not construct a dynamic proxy for an abstract class, so if you think it will be useful for users to make dynamic proxies that is one reason to favour an interface.
Last word: Plan your interfaces logically. Use only if you really need to.
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Using interfaces in APIs - II