j rab wrote:why would default methods be needed? Can you explain this to me?
Let's add an illustrative code snippet. Imagine you are into card games and decide to create a framework/library/application where anybody can create the deck of his/her favourite card game. So you create an enum
Suit, an enum
Rank, and a class
Card. Next on the list is an interface Deck with some useful methods
And you already release this API while you'll develop the code for the GUI. Many people like this library and start developing concrete Deck classes for their favourite card game. And some of these developers provide some feedback to you: it would be nice to have an add method for a single card and one for a single deck. So you alter the Deck interface and add two methods
And you release a new version of this API. But what happens if people update to this new version? They all get compiler errors, because the concrete
Deck classes don't implement these two new methods. So no backwards compatibility and everybody needs to update their classes. How could you ensure backwards compatibility and assure no code updates are required? Using default interface methods! Here is the improved
Deck interface
Hope it helps!
Kind regards,
Roel