Backward compatibility issue with java 8: new methods in JDK

Simple question. In Java 8, we have a huge number of new methods in the JDK classes. Let's say we created such a class using Java 7 (or Java 6):

class MyArrayList<E> extends ArrayList<E> {
        public void sort(Comparator<E> c) {
            // some sort
        }
}

This is a very reasonable implementation. Now we try to compile it with Java 8 and get the expected compilation error:

error: name clash: sort(Comparator<E#1>) in MyArrayList and sort(Comparator<? super E#2>) in ArrayList have the same erasure, yet neither overrides the other
            public void sort(Comparator<E> c) {
                        ^   where E#1,E#2 are type-variables:
    E#1 extends Object declared in class I.MyArrayList
    E#2 extends Object declared in class ArrayList

Here I would like to ask 2 questions:

  • Even with a parameter javac -source 1.7 -target 1.7using JDK 8, I get the same error - why? I thought that these options should allow compiling legacy code.

  • How about backward compatibility in general?

EDIT To be precise, maybe I'm doing something wrong? JDK 1.8.0_65, Mac OS X:

bash-3.2$ javac -version
javac 1.8.0_65
bash-3.2$ javac -source 1.7 -target 1.7 MyArrayList.java 
warning: [options] bootstrap class path not set in conjunction with -source 1.7
MyArrayList.java:7: error: name clash: sort(Comparator<E#1>) in MyArrayList and sort(Comparator<? super E#2>) in ArrayList have the same erasure, yet neither overrides the other
    public void sort(Comparator<E> c) {
                ^
  where E#1,E#2 are type-variables:
    E#1 extends Object declared in class MyArrayList
    E#2 extends Object declared in class ArrayList
1 error
1 warning
+4
2
  • Java 8. JDK , JDK. , Java 7 , , - Java 7. JDK 7 ( -bootclasspath) -.

  • , . , , .

+6
  • -source 1.7 , Java 7. -target 1.7 , - JVM. , - JDK 8. -, javac, Java 7 -bootclasspath -extdirs
  • ( Java 8) . , ( ). .
+7

Source: https://habr.com/ru/post/1624014/


All Articles