Search code examples
javajavac

unable to use an intersection type when notional class requires access modification


Interfaces:

interface PublicCloneable {
    Object clone();
}

interface HasPosition {
    // doesn't matter
}

Attempt to use intersection type:

@SuppressWarnings("unchecked")
<E extends PublicCloneable & HasPosition> E cloneAndIncrementPosition(E elem) {
    final E clone = (E)elem.clone();
    // rest omitted
}

Attempt to compile with javac 1.8.0_60:

$ javac xx.java
xx.java:13: error: clone() in Object cannot implement clone() in PublicCloneable
    <E extends PublicCloneable & HasPosition> E cloneAndIncrementPosition(E elem) {
     ^
  attempting to assign weaker access privileges; was public
xx.java:14: error: clone() has protected access in Object
        final E clone = (E)elem.clone();
                               ^
2 errors

Why this intersection type is invalid for javac?


Solution

  • This looks like a javac bug.

    http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.4

    The members of a type variable X with bound T & I1 & ... & In are the members of the intersection type (§4.9) T & I1 & ... & In

    http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.9

    Every intersection type T1 & ... & Tn induces a notional class or interface for the purpose of identifying the members of the intersection type ...

    If Ck is Object, a notional interface is induced ... has direct superinterfaces T1', ..., Tn'

    Therefore, for PublicCloneable & HasPosition, a notional interface is introduced, extending both of them, which should be OK.