BinaryCompatibleDefaultMethods

From APIDesign

Revision as of 06:02, 28 September 2020 by JaroslavTulach (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

DefaultMethods are useful when one desperately needs to add a method into an existing interface. However, they decrease clarity of a ProviderAPI. As such, don't overuse. Morever it has been recently demonstrated that adding DefaultMethods can even compromise BinaryCompatibility.


Recently Emilian Bold asked me to participate in a tweeting about binary incompatibility caused by adding CharSequence.isEmpty in JDK15. An interesting case. Following code compiles on JDK8 to JDK14:

public interface ArrayLike {
  int length();
 
  default boolean isEmpty() {
    return length() == 0;
  }
}
 
class CharArrayLike implements CharSequence, ArrayLike {
  private final char[] chars;
 
 
}
Personal tools
buy