Carl Smith wrote:
In Java/J2EE community, it seems that a lot of experienced developers tend to use a lot of interfaces, however, a lot of junior developers ignore using interface. I am not sure why interfaces seem to be favorite to some experienced developers. Can some one explain this?Can you give examples where an interface is preferred?
Much more importantly than 'because Java doesn't have multiple inheritance' is what Dave's alluding to, that if you code to interfaces the implementation can be switched without impact. As a more general example, consider if you wrote all your code to use ArrayList and later found, after profiling, that you needed to switch to LinkedList for performance reasons. You'd have to update all your code -- including all the clients of all the methods that accepted or returned an ArrayList.
Conversely, if you coded to the List interface, you would only need to change the implementation code. All the client code would be unaffected. That's Dave's point about being able to switch X for Y with minimal impact.
In 'text book' terms, extending a base class expresses an IS-A relationship where implementing an interface expresses a HAS-A (behaviour) relationship. Inheritance is a very over-used language feature; often it's used to express a 'has some of the same behaviour as, even if it's semantically unrelated' relationship -- i.e. as an implementation convenience rather than an expression of model semantics. Use of interfaces can go a long way to providing cleaner object models.
Just my 2c... -- Laurie, Open Source advocate, Java geek and novice blogger: http://www.holoweb.net/laurie --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]