On 8/10/05, Laurie Harper <[EMAIL PROTECTED]> wrote: > 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.
And to answer the original question to why experienced developers tend to use interfaces more often than newbies: a seasoned programmer knows that eventually their code needs to be updated, refactored or otherwise changed. Hence in the long run, the extra effort it takes to design the interface will pay off. The newbie just assumes that he will get it right the first time. Ulrich --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]