dsplynm
dsplynm

Reputation: 613

Java 7 intersection types: what exactly does the specification say?

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

I would have expected the following code to cause a compile-time error, but it doesn't:

public interface I1 {}
public interface J1 extends I1 {}
public interface J2 {}
public interface J3 extends J2 {}

class C implements J1, J3 {}

public class A<T extends J1 & J3> {
    public static void main(String[] args) {
        A<C> a = new A<>();
    }

}

As far as I understand, the Ti <: Ci types are as follows:

Now, I would need to have a Tk <: Ck, where Ck <: C1 and Ck <: C2, but if Ck := C1, then I1 <: J2 is false, and if Ck := C2, then J2 <: I1 is false.

What am I missing here?

Upvotes: 4

Views: 2022

Answers (1)

assylias
assylias

Reputation: 328785

You seem to confuse <: and <. If you read the next section about subtyping, you will see that <: is valid for a class itself - in other words, for any type T, T <: T is true. This is as opposed to < which is used for strict subclassing (T < T is always false).

In your example:

  • T1 == C1 == J1
  • T2 == C2 == J2
  • Tk == Ck == C

and you can verify that C <: J1 and C <: J2.

So it all looks fine.

Upvotes: 4

Related Questions