简体   繁体   中英

Can interfaces contain methods that have an implementation?

The Oracle docs say the following, "Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation . However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods..."

I thought interfaces could only contain abstract methods and abstract methods, by definition, have no implementation. So why do the Java docs say abstract classes are similar to interfaces in that they may contain a mix of methods declared with or without an implementation?

link to the docs

Because

I thought interfaces could only contain abstract methods and abstract methods

is incorrect. Or rather, it was correct until java 1.7, and has been incorrect since then, as java8 introduced the interface default methods mechanism . In other words, it has been incorrect for about 15 years at this point.

This is legal java, now:

interface Map<K, V> {
  public V get(Object key);
  public boolean containsKey(Object key);

  public default V getOrDefault(Object key, V defaultValue) {
    return containsKey(key) ? get(key) : defaultValue;
  }
}

default methods blur the lines more between abstract classes and interfaces. Any implementation can override them if it wants. A few rules about them:

  • There is no hierarchy to these. If you define a class that ends up implements ing more than one interface, and this results in inheriting more than one such 'default implementation' for the same method, the compiler will refuse to compile this , and the only way out is to explicitly define that method. You can refer to whatever impl you want using InterfaceName.super.methodName(params); if you like. This 'solves' the "diamond problem" this causes. In other words, the ordering in which you place your interface types in an implements clause still does not matter.
  • interfaces still can't declare fields (or rather, they become constants - implicitly public static final ).
  • They are intended for adding utility methods in a version update that are built on top of existing methods, such as getOrDefault (which actually exists ) and has done so since java 8). Generally they aren't really 'meant' for expanding on what an interface represents, given that you can't wave a magic wand and get implementors to just grow implementations out of nowhere. In rare cases you can expand on what an interface represents based entirely on default behaviour that doesn't involve any fields. In that specific case, you could use the default mechanism too.
  • The default keyword is required.
  • The same update also included the ability to stick static methods in them, which is nice.
  • You can also write private methods now. This functionality exists solely to let you write helper methods for your default methods.

Default methods are a way to put implementation within an interface.

But be aware: Default methods were added to Java to solve the problems of adding support for lambdas without breaking the backward-compatibility for which Java is so famous.

Generally, adding a default method should be a last resort , not your first option.

Usually best to regard an interface as strictly a contract , a promise to be made by a separate class to implement certain behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM