"both methods have same erasure" error using bounded type parameters

Lapo :

I'm using generics in Java for the first time, and I'm facing an issue I don't manage to overcome: why this compiles:

public interface Aa{}
public interface Bb{}
public interface Cc{}


public static <GenericAB extends Aa & Bb>
void method(GenericAB myABobject1, GenericAB myABobject2){}

public static <GenericAB extends Aa & Bb, GenericCA extends Cc & Aa>
void method(GenericAB myAbobject, GenericCA myCAobject){}

But this does not:

public interface Aa{}
public interface Bb{}
public interface Cc{}


public static <GenericAB extends Aa & Bb>
void method(GenericAB myABobject1, GenericAB myABobject2){}

public static <GenericAB extends Aa & Bb, GenericAC extends Aa & Cc>
void method(GenericAB myAbobject, GenericAC myACobject){}

And I get this error: both methods have same erasure.

I'm sorry if this is a stupid question, but I don't get why the order of interfaces in a bounded type parameter declaration seems to have importance. In reality I don't think it is the order which causes the error, but I don't get what does.

I'm reading this documentation by Oracle, it says which I must put the class as first parameter but Aa, Bb and Cc are all interfaces. Sorry for my english too.

Radiodef :

It is the order that matters (§4.6):

The erasure of a type variable (§4.4) is the erasure of its leftmost bound.

GenericBC erases to either Aa or Cc, depending on which appears first (i.e. leftmost) in the bound.

Also see type erasure tutorial and type erasure, when and what happens Q&A for explanations of type erasure in general.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=456858&siteId=1