Question

public abstract class SomeClass<Sometype extends OtherClass> extends Something   implements   Comparator<??>{

//code

How can I replace ?? in Comparator such that it is any class that extends SomeClass?

Was it helpful?

Solution

It's not clear if you want to carry the type-bounds or simply allow any subclass of SomeClass.

By definition, the compare method you implement will allow comparison with any subtype (because any subtype can be cast to the more general parent type when passed to the method).

To allow any parameterized type instead of the restrictive Sometype, you can write:

public abstract class SomeClass<Sometype extends OtherClass> extends Something
   implements Comparator<SomeClass<?>>

It's also legal to write this with explicit type bounds:

public abstract class SomeClass<Sometype extends OtherClass> extends Something
   implements Comparator<SomeClass<Sometype>>

OTHER TIPS

you cant use something like ? extends SomeClass , but you can use just Comparator<SomeClass> and cast to what you need

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top