1

I've been reading a few articles about the new Java 8 features. With the introduction of Extension Methods i.e. methods that are prefixed with the default keyword and provide an implementation.

I checked a couple of questions here where people have asked about the differences in abstract classes and interfaces like the one here and here and the biggest difference seems to be that abstract classes allowed you to specify default implementations.

Does this mean that the difference between Abstract Classes and Interfaces is largely cosmetic and given the fact that a class can implement multiple interfaces; they give more flexibility in both design and implementation.

I can see that abstract classes are here to stay, primarily for being backward compatible with older code. Would I be right in not using Abstract Classes going forward and use Interfaces only? If not could you provide an example, just having some fields defined in the abstract class doesn't count. I'm looking for a compelling use case where Abstract Classes are still a better fit when compared to Interfaces.

On the flip side the purists would argue that the default implementations pollute the interfaces which should only specify the contract and the behavior should always be in the Concrete classes(A part of the behavior could be in abstract classes). Is there any merit to this argument?

Community
  • 1
  • 1
nikhil
  • 8,925
  • 21
  • 62
  • 102
  • 1
    They certainly aren't similar enough to make abstract classes useless. – Zach Thacker Mar 28 '14 at 19:22
  • 1
    I feel that this is a non-question. Interfaces and abstract classes are two different tools, both of which are needed in the toolbox. A flat-head screwdriver CAN be used to kind of screw in philips screws. Should I therefor chuck out my philips screw driver? The point is sort of moot imo. – Wim Ombelets Mar 28 '14 at 19:25

1 Answers1

10

Abstract classes can have constructors and states. Even with extension methods an interface cannot have any state. So for example, you would still need an Abstract class for something like modeling the basic "size" state for a #getSize or #setSize method.

robert_difalco
  • 4,821
  • 4
  • 36
  • 58