The new stream() method by default in Collection returns Stream<E> , as well as a new type in Java 8. Outdated code will not compile if it contains the stream() method with the same signature, but returning something else, which leads to collision of return types.
Outdated code will continue to work until it is recompiled.
First, in 1.7, set the following:
public interface MyCollection { public void foo(); } public class Legacy implements MyCollection { @Override public void foo() { System.out.println("foo"); } public void stream() { System.out.println("Legacy"); } } public class Main { public static void main(String args[]) { Legacy l = new Legacy(); l.foo(); l.stream(); } }
With -source 1.7 -target 1.7 , this compiles and runs:
$ javac -target 1.7 -source 1.7 Legacy.java MyCollection.java Main.java $ java Main foo Legacy
Now in 1.8, add the stream method to MyCollection .
public interface MyCollection { public void foo(); public default Stream<String> stream() { return null; } }
We compile only MyCollection in 1.8.
$ javac MyCollection.java $ java Main foo Legacy
Of course, we can no longer recompile Legacy.java .
$ javac Legacy.java Legacy.java:11: error: stream() in Legacy cannot implement stream() in MyCollection public void stream() ^ return type void is not compatible with Stream<String> 1 error
source share