Question from a book:
In the past (pre-Java 8), you were told that it’s bad form to add methods to an interface because it would break existing code. Now you are told that it’s okay to add new methods, provided you also supply a default implementation.
- How safe is that? Describe a scenario where the new
stream
method of theCollection
interface causes legacy code to fail compilation.- What about binary compatibility? Will legacy code from a JAR file still run?"
My answers are as follows but I am not quite sure about them.
- It's safe only if legacy code does not provide a method with the same name
stream
and with the same signature (e.g. in a legacy class that implementsCollection
). Otherwise, this old legacy code will fail compilation. - I think binary compatibility is preserved, legacy code from old JAR file will still run. But I have no clear arguments at all about this.
Could anyone confirm or reject these answers, or just add some more arguments, references, or clarity to these answers?