Java Magazine, Jan/Feb 2018
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2018 98 new to java void doMagic This interface has already been used and implemented by many classes in many projects But you now come up with some really great new functionality and you would like to add a really useful new method public interface MagicWand void doMagic void doAdvancedMagic If you do that then all classes that previously implemented this interface break because they are required to provide an implementation for this new method So at first glance it seems you are stuck either you break existing user code which you dont want to do or youre doomed to stick with your old libraries without a chance to improve them easily In reality there are some other approaches that you could try such as extending interfaces in subinterfaces but these have their own problems which I do not discuss here Java 8 came up with a clever trick to get the best of both worlds the ability to add to existing interfaces without breaking existing code This is done using default methods and static methods which I discuss now Default Methods Default methods are methods in interfaces that have a method body the default implementation They are defined by using the default modifier at the beginning of the method signature and they have a full method body public interface MagicWand
You must have JavaScript enabled to view digital editions.