2

Normally, an interface would be frozen once released into production.

Hence, if you need added functionality, your option in Java would be to extend an existing interface into a new interface, which describes the added functionality.

This ensures backward compatibility in the sense that you do not break the contract specified in the original interface, and you are free to implement the new interface to describe that you a new version of a particular class with additional functionality.

However, in Java 8 default method implementations were introduced to interfaces, enabling the specification of additional methods in existing interfaces to ensure backwards compatibility. However, I do not see why you would opt for this option over the previous one - and while doing so open up for serious mistakes in terms of creating poor quality code.

Could someone elaborate on this?

  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/190728/discussion-on-question-by-anders-clausen-why-were-default-methods-included-inter). – Samuel Liew Mar 26 '19 at 22:04

1 Answers1

0

It is obvious that the most important reason for introducing default methods in Java8 is backward compatibility. As you mentioned, before this feature, to add a new method to the interface class we should create an inherited class and it is absolutely not an effective way. Because it made developers change all object types if they wanted to use newly developed features.

To understand the importance of the default method, consider how much changes were needed to add forEach method to Iterable interface. As you know Iterable is the parent of Collection which is the parent of all existing type of lists in java. What a huge change needed to add a simple method to the Iterable without introducing the default method!

In addition, right now I'm benefiting default methods to reduce code writing and I don't know why it is not mentioned on any websites. A method behavior can be the same in some inherited classes and I can easily implement the behavior in parent interface using the default method and clearly change the behavior everywhere I want. However this is not a big deal, but it really makes me comfortable with development.

Hamid Ghasemi
  • 880
  • 3
  • 13
  • 32
  • But, if you want to make use of a new feature, you are changing anticipation towards an abstraction. That is exactly the reason that you can even extend on interfaces in OOP. So, I don't see why this would constitute an ineffective way? Indeed you would have to change the type everywhere you would want to use the new feature, but arguable that would be a small change compared to the change that you would need to make to make use of new features. – Anders Clausen Mar 26 '19 at 09:39
  • As for the forEach point: If your collections share common implementations (as is the case with the forEach method), what you should do as per the OOP paradigm is to introduce a common parent class (abstract in this case), and have all of the concrete classes (here, ArrayList, LinkedList, HashSet etc..) extend on that class. If you need further shared functionality in the future, the abstract parent class will implement the interfaces describing this new functionality. – Anders Clausen Mar 26 '19 at 09:44
  • As for you last point, that reusability can easily be achieved with abstract classes and optional overriding? If you are having multiple interfaces with default behavior implemented in a single class, you are effectively using multiple inheritance which there are multitudes of reasons to avoid. – Anders Clausen Mar 26 '19 at 09:45