I am trying to understand where Interfaces should be used and where Abstract Classes. What are the advantages of one over the other.
I have read Eckel's book on this topic but don't find it quite satisfactory. Can somebody guide to some source or explain it themself.
Joined: May 31, 2007
Q. When to use an abstract class?: In case where you want to use implementation inheritance then it is usually provided by an abstract base class. Abstract classes are excellent candidates inside of application frameworks. Abstract classes let you define some default behavior and force subclasses to provide any specific behavior. Care should be taken not to overuse implementation inheritance.
Q. When to use an interface?: For polymorphic interface inheritance, where the client wants to only deal with a type and does not care about the actual implementation use interfaces. If you need to change your design frequently, you should prefer using interface to abstract. Coding to an interface reduces coupling and interface inheritance can achieve code reuse with the help of object composition. For example: The Spring framework�s dependency injection promotes code to an interface principle. Another justification for using interfaces is that they solve the �diamond problem� of traditional multiple inheritance as shown in the figure. Java does not support multiple inheritance. Java only supports multiple interface inheritance. Interface will solve all the ambiguities caused by this �diamond problem�.
Design pattern: Strategy design pattern lets you swap new algorithms and processes into your program without altering the objects that use them. Strategy design pattern: Refer Q11 in How would you go about� section.
Note that this question is asked about one a week on this forum, so there is not much new ground to cover here, but its worth mentioning that abstract classes can implement an interface which can give you the best of both worlds. I would say that when you get the urge to implement an abstract class you should define an interface first and let the abstract class implement that. That gives you the option of whether you want to tie your new class to the abstract class hierarchy (possibly with default implementation of some methods), or code an implementation of the interface from scratch. That way you gain a lot of freedom. All code not in the inheritance hierarchy should refer only to the interface.
I'm sure whoever coded the java.util.Stack class wishes they would have defined behavior in terms of an interface instead of directly extending java.util.Vector (slightly off-topic since Vector is not an abstract class).
Does that make sense?
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus