In case where you want to use implementation inheritance then it is usually provided by an abstract base class
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.
Rohan Deshmkh wrote:I could not understand the line about interface inheritance. Also why should we prefer interface over abstract classes when we want to frequently change our design?
Campbell Ritchie wrote:Which book? You should always tell us where such quotes come from.
Paul Witten wrote:
What they mean is "if you are forced to redesign your class often then that indicates that a polymorphic interface(s) would be preferable."
Campbell Ritchie wrote:Thank you for quoting the book.
I don’t know whether our FAQ would help you. I did not think that passage from the book is very clear; it might be clearer in the context of the whole paragraph, however.
I don’t know whether this sort of example helps, but let’s try:-
Q. When to use an abstract class?:
In case where you want to use implementation inheritancethen 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 as discussed in Q10in Java section.
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. COCoding to an interfacereduces coupling and
interface inheritance can achieve code reusewith 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
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
Jeff Verdegan wrote:Interfaces don't solve the diamond problem. The lack of multiple inheritance of implementation solves some of the diamond problem. But using interfaces can still have its own diamond problem, at least in theory, though I've never run across it in practice.
Our CowBartOyIst inherits from two parent types (yes, interface implementation is inheritance), so it's a potential diamond problem.
Jeff Verdegan wrote:. Is there some reasonable common/default behavior that could be provided for at least some implementations? Then use abstract class. Are we defining a pure abstract type? Then use interface. And in many cases we use both.
Rohan Deshmkh wrote:
I understood about when to use abstract class but i am not able to understand this line:
Are we defining a pure abstract type? Then use interface.
Can you give an example about pure abstract type?