2

I read all the other questions about this, but i can't understand why there is type erasure in my case. I have

public <T extends Aclass&Binterface> boolean test(final T param) { ... } public boolean test(final Aclass pOtherPPShape) { ... } 

Method test(Aclass) has the same erasure test(Aclass) as another method in type ClassName

How should i write two methods that operate on the same class but one with an interface and one not? (i can't modify the AClass since is from an external library, and have a lot of subtypes with which this code should work)

Is the use of instanceof right in this case?

EDIT: i use this in a collision test. If the shape passed is instance of an interface, i get a collisionmask(with a method of the interface) and then call the "checker", if it's not, i want to instantiate a dummy collisionmask that does some different stuff and pass to the same method of the "checker" (that has as parameters the 2 shapes and 2 collisonmasks)

3
  • What is it you want to achieve here? Can you stub-out what you'd like to be doing? Commented Jul 31, 2012 at 19:42
  • whats about defining the second method public <T extends Aclass> boolean test(final T param) Commented Jul 31, 2012 at 19:46
  • They'll still have the same erasure. Commented Jul 31, 2012 at 19:46

1 Answer 1

1

You've recognized that the issue is about type-erasure, and that you can't "overload" on the one parameter with type-erasure in the way (type-erasure - you will be assimulated). But, you say,

The generic type signatures are distinct, why did you get a conflict?

The two types T extends Aclass&Binterface and Aclass are distinct, aren't they?

Well, yes and no. While in truth they are distinct, under type-erasure they're not distinct**1. An excellent resource explains: "In the process of type erasure the compiler replaces type parameters by their leftmost bound". Which in your case was Aclass, which led to the conflict between the two methods.

The explanation suggests a ...

Solution: formalize the combound**2 generic type declaration to establish a unique leftmost bound

Replace <T extends Aclass&Binterface> with <T extends AandB>, where AandB is created thus:

  1. Define interface Ainterface with Aclass method signatures

  2. Have Aclass implement Ainterface

  3. Define interface AandB extends Ainterface, Binterface.

OR

you can use instanceof.


**1 "Because the longer wave lengths are refracted by the particles in the atmosphere", per Duncan Jones, another excellent resource.

**2 That'd be compound, cause I couldn't resist the fun :-/

Sign up to request clarification or add additional context in comments.

6 Comments

Ok, but why does type erasure happens? Doesn't the compiler know when it compiles the code if the class is an instance of the Binterface or not, does it? A method with a class as a parameter and a class that also implements an interface is treated the same way?
Every time i have to do with java generic is fell like they are constricting.. Btw thanks, at the end i choose to use instanceof check: much simpler and much more readable code
@RichardSitze Because the longer wave lengths are refracted by the particles in the atmosphere ;-)
@DuncanJones Thanks, I've update my response to include your insights!
@Makers_F And now I have, I think, a final answer as to why
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.