Erasure vs implicit cast for multiple bounded types in a Generic Class/Method

118 Views Asked by At

According to JLS§4.4:

The order of types in a bound is only significant in that the erasure of a type variable is determined by the first type in its bound, and that a class type or type variable may only appear in the first position.

Consider the following snippet:

package TypeVarMembers;

class C { 
    public    void mCPublic()    {}
    protected void mCProtected() {} 
              void mCPackage()   {}
    private   void mCPrivate()   {} 
} 

interface I {
    void mI();
}

class CT extends C implements I {
    public void mI() {}
}

class Test {
    <T extends C & I> void test(T t) {  
        t.mI();           // OK
        t.mCPublic();     // OK 
        t.mCProtected();  // OK 
        t.mCPackage();    // OK
        //t.mCPrivate();    // Compile-time error
    } 
}

Here are some doubts that I have-:

  1. Erasure for method test(T t) => test(C t)?
    • if this is the case - then why are we dropping the additional types while computing Erasure?
  2. Implicit cast for the type T in method test(T t) => (C & I)?
  3. Theoretically is the erasure same as the implicit cast?
0

There are 0 best solutions below