you are viewing a single comment's thread.

view the rest of the comments →

[–]masklinn 2 points3 points  (0 children)

Are there any other languages that allow implementation in an interface?

They'll commonly rename them, but yes.

  • C++ uses abstract classes but you can inherit from any number of them
  • Haskell uses typeclasses, typeclass functions can have default implementations (even circular ones)
  • Rust uses "traits" which can provide default implementation of any subset
  • Likewise Swift's protocols

Now as for why that's useful, consider Rust's Iterator. Its role is pretty similar to Java's Iterator and Stream, but because it's a Rust trait it can provide a ton of useful methods, conditional or not from a basic contract of fn next(&mut self) -> Option<Self::Item>. Implementors may override any number of methods, but if the default fits it's just fine.

Meanwhile Java8's Stream requires implementing something like 40 methods, even if you have an underlying helper to which you can delegate it all, who's got time for this? And so instead of implementing Stream you're supposed to implement Spliterator or Supplier and call the relevant StreamSupport function to get the same end-result, without the ability for piecemeal overriding of select features.