Difficult condition for understanding by Java specification

when reading the reference type in the Java SE specification:

Given the link type of compile time S (source) and compile time reference type T (target), cast conversion exists from S to T if compilation errors do not occur due to the following rules.

I keep finding the following condition:

If S is a class type: If T is a class type, then either |S| <: |T| |S| <: |T| or |T| <: |S| |T| <: |S| . Otherwise, a compile-time error occurs.

Furthermore, if there is a supertype X from T and a supertype Y from S such that both X and Y are provably distinct parameterized types (ยง4.5), and that the erasures of X and Y are the same, compilation time fails.

Can someone give me an example of this situation?

EDIT:

For further clarification of the article I am quoting, refer to section 5.5.1 on this link

+7
java specifications
source share
1 answer

The first part of the condition requires that either S <: T or S :> T , that is, one class must inherit from another; otherwise there will be a compile-time error. So, your basic setup is as follows:

 class T { } class S extends T { } 

So far, so good: you are allowed to drop S into T because there is a corresponding subclass relation between the two classes.

Now let's look at the second part of the condition: two classes must have different supertypes. Since only one superclass is allowed, the common supertype must be an interface. Here is one example of how to break the second part of a rule:

 // X is List<String> class T implements List<String> { } // Y is List<Integer> class S extends T implements List<Integer> { } 

Erasing both X and Y must implement List<???> , but the list must be parameterized with a different type. This causes a compile-time error because S unable to satisfy the List<String> and List<Integer> interfaces.

+3
source share

All Articles