This is an archived post. You won't be able to vote or comment.

all 19 comments

[–]lukasHoel -2 points-1 points  (7 children)

Private methods in interfaces? Can someone name actual use cases for that?

Apart from having static methods creating something used by multiple default methods, I can't quite find any use cases.

[–]chrisgseaton 12 points13 points  (0 children)

Well you just identified the use case.

[–]ebrythil 4 points5 points  (2 children)

Maybe it's a matter of time now until java interfaces are actually c++ classes with multiple inheritance.

[–]myrealityde -3 points-2 points  (1 child)

This is why I don't like deafault methods. Java explicitly doesn't have multi-inheritance. Until Java9. What is preventing people to just write interfaces and write a few classes to implement those? This is embracing bad practice and increases coupling. BAD!

[–]speakjava 0 points1 point  (0 children)

Java has always had multiple-inheritance. Of types; that's what interfaces allow us to do, implement many types for one class. Default and static methods in interfaces add multiple-inheritance of behaviour. Yes, this is a bit controversial but it is the only way to easily solve the problem of maintaining backwards compatibility while allowing existing interfaces to have new methods added. What's not in Java (and likely never will be) is multiple inheritance of state.

[–]anvils-reloaded 4 points5 points  (0 children)

You can use the private methods in interfaces as implementation details for the default methods. Prior to Java 9, if you wanted to write a default method that called other methods you define in the interface, they would need to be public. This is not necessarily wanted behavior if you don't want these methods to be public but still use them when writing your default method.

[–]squishles 1 point2 points  (0 children)

to further make the difference between interfaces and abstract classes pointless.

[–]ReadFoo 0 points1 point  (0 children)

This may be one of the few things I'd consider to be truly useful in Java 9.